开发者

Problem with OleDbConnection string - folder name contain white space

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.


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());


Put [] around the file:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜