【问题标题】:Problem with OleDbConnection string - folder name contain white spaceOleDbConnection 字符串的问题 - 文件夹名称包含空格
【发布时间】:2011-08-08 14:51:56
【问题描述】:

我对 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 - 包含空格

如果从该文件夹名称中删除空格,则效果很好。

但是如何解决呢?更改文件夹名称对我来说不是解决方案。

【问题讨论】:

标签: c# excel connection-string whitespace oledbconnection


【解决方案1】:

您可以尝试使用OleDbConnectionStringBuilder 类:

var sb = new System.Data.OleDb.OleDbConnectionStringBuilder();
sb.Provider = "Microsoft.Jet.OLEDB.4.0";
sb.DataSource = @"E:\C# PROJECTS\AUSK\T-TOOL\T-TOOL\bin\Release\Config\simcard.xls";
sb.Add("Extended Properties", "Excel 8.0");
MessageBox.Show(sb.ToString());

【讨论】:

    【解决方案2】:

    将 [] 放在文件周围:

    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                             "Data Source=[" + file + "];" +
                             "Extended Properties=Excel 8.0;";
    

    【讨论】:

      【解决方案3】:

      在使用上述两个建议失败后,我将添加自己的经验。上面的第一个解决方案是将“Provider”设置为“DataSource”属性,而第二个不适合 Microsoft.ACE.OLEDB.12.0 提供程序,因为它们使用引号而不是括号作为文件名附件。所以,我的(经过测试的)解决方案是:

      Dim sb As OleDbConnectionStringBuilder = New System.Data.OleDb.OleDbConnectionStringBuilder()
      sb.Provider = "Microsoft.ACE.OLEDB.12.0"
      sb.DataSource = "c:\datafile.accdb"
      sb.OleDbServices = -1
      Using connection As New OleDbConnection(sb.ToString())
      ....
      End Using
      

      这以一个字符串结束(注意引号): Provider=Microsoft.ACE.OLEDB.12.0;Data Source="c:\datafile.accdb";OLE DB Services=-1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        • 2017-08-16
        • 2012-09-30
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        相关资源
        最近更新 更多