Format of the initialization string does not conform to specification starting at index 128

十年热恋 提交于 2019-12-13 04:33:15

问题


Mine is C# windows application. When i run this application in my local machine it gives following error:- "Format of the initialization string does not conform to specification starting at index 128".

try
{
    string path = System.IO.Path.GetFullPath("E:\\09-2013\\SalesRep\\Openleads.xlsx");
    if (Path.GetExtension(path) == ".xls")
    {
        oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; 
        Data Source=" + path + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
    }
    else if (Path.GetExtension(path) == ".xlsx")
    {
        oledbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; 
        Data Source=" + path + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
    }

    oledbConn.Open();
    OleDbCommand cmd = new OleDbCommand(); ;
    OleDbDataAdapter oleda = new OleDbDataAdapter();
    DataSet ds = new DataSet();

    cmd.Connection = oledbConn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT * FROM [Raw Data$]";
    oleda = new OleDbDataAdapter(cmd);
    oleda.Fill(ds, "dsSlno");
    grvData.DataSource = ds.Tables["dsSlno"].DefaultView;
    oleda = new OleDbDataAdapter(cmd);
    oleda.Fill(ds);

    grvData.DataSource = ds.Tables[1].DefaultView;
}
catch (Exception ex)
{

}
finally
{
    oledbConn.Close();
}

回答1:


Your path needs an @ sign in the front of it to escape your backslashes:

There is a null-terminating character (\0) after the 'E:' in it causing your issue when it's concatenated to your connection string.

Change this:

string path = System.IO.Path.GetFullPath("E:\09-2013\SalesRep\Openleads.xlsx");

To this:

string path = System.IO.Path.GetFullPath(@"E:\09-2013\SalesRep\Openleads.xlsx");

And you should be fine.



来源:https://stackoverflow.com/questions/18636261/format-of-the-initialization-string-does-not-conform-to-specification-starting-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!