OleDbConnection 字符串问题 - 文件夹名称包含空格

发布于 2024-11-28 14:45:55 字数 1596 浏览 11 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请叫√我孤独 2024-12-05 14:45:55

您可以尝试使用 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());

You can try using the OleDbConnectionStringBuilder class:

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());
等风也等你 2024-12-05 14:45:55

将 [] 放在文件周围:

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

Put [] around the file:

string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                         "Data Source=[" + file + "];" +
                         "Extended Properties=Excel 8.0;";
清风挽心 2024-12-05 14:45:55

在使用上述两个建议失败后,我将添加自己的经验。上面的第一个解决方案是将“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

这最终形成一个类似的字符串(注意引号):
提供程序=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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文