OleDbConnection 字符串问题 - 文件夹名称包含空格
我对 OleDbConnection 字符串格式有问题。我使用 OleDb 类来访问 Excel 文件。
这是将 Excel 表加载到数据集的方法。
public DataSet LoadExcelFileToDataSet(string file,
string sheetName)
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file + ";" +
"Extended Properties=Excel 8.0;";
var oledbConn = new OleDbConnection(connString);
try
{
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet Sheet1
var cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "$]", oledbConn);
// Create new OleDbDataAdapter
var oleda = new OleDbDataAdapter { SelectCommand = cmd };
// Create a DataSet which will hold the data extracted from the worksheet.
var ds = new DataSet();
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds, "SIMCards");
return ds;
}
catch(Exception ex)
{
throw ex;
}
finally
{
// Close connection
oledbConn.Close();
}
}
这个方法效果很好。问题是,如果我尝试在 WPF 应用程序中使用此方法和相对路径。
LoadExcelFileToDataSet(Config\\simcard.xls,sheetName)
完整路径为: E:\C# PROJECTS\AUSK\T-TOOL\T-TOOL\bin\Release\Config\simcard.xls
问题是此文件夹名称 C# PROJECTS - 包含空格
如果删除白色该文件夹名称中包含空格,效果很好。
但如何解决呢?更改文件夹名称对我来说不是解决方案。
I have problem with OleDbConnection string format. I use OleDb classes on access to Excel file.
Here is method wich load excel table to dataset.
public DataSet LoadExcelFileToDataSet(string file,
string sheetName)
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file + ";" +
"Extended Properties=Excel 8.0;";
var oledbConn = new OleDbConnection(connString);
try
{
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet Sheet1
var cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "$]", oledbConn);
// Create new OleDbDataAdapter
var oleda = new OleDbDataAdapter { SelectCommand = cmd };
// Create a DataSet which will hold the data extracted from the worksheet.
var ds = new DataSet();
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds, "SIMCards");
return ds;
}
catch(Exception ex)
{
throw ex;
}
finally
{
// Close connection
oledbConn.Close();
}
}
This method works good. Problem is if I try use this method with relative path in WPF app.
LoadExcelFileToDataSet(Config\\simcard.xls,sheetName)
full path is : E:\C# PROJECTS\AUSK\T-TOOL\T-TOOL\bin\Release\Config\simcard.xls
Problem is this folder name C# PROJECTS - contains white space
If remove white space from this folder name, it works good.
But how to solve it? Change folder name is not solution for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用 OleDbConnectionStringBuilder 类:
You can try using the OleDbConnectionStringBuilder class:
将 [] 放在文件周围:
Put [] around the file:
在使用上述两个建议失败后,我将添加自己的经验。上面的第一个解决方案是将“Provider”设置为“DataSource”属性,而第二个解决方案不适合 Microsoft.ACE.OLEDB.12.0 提供程序,因为它们使用引号而不是括号作为文件名封装。所以,我的(经过测试的)解决方案是:
这最终形成一个类似的字符串(注意引号):
提供程序=Microsoft.ACE.OLEDB.12.0;数据源=“c:\datafile.accdb”;OLE DB 服务=-1
I'm going to add my own experience after I failed using the two suggestions above. The first solution above is setting "Provider" as "DataSource" property while the second is not suitable for Microsoft.ACE.OLEDB.12.0 provider because they are using quotes not brackets as file name enclosures. So, my (tested) solution was:
This ended up in a string like (note the quotes):
Provider=Microsoft.ACE.OLEDB.12.0;Data Source="c:\datafile.accdb";OLE DB Services=-1