问题描述
我的是C#Windows应用程序.当我在本地计算机上运行此应用程序时,出现以下错误:-初始化字符串的格式不符合从索引128开始的规范."
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();
}
推荐答案
您的路径需要在其前面加上@符号,以避开反斜杠:
Your path needs an @ sign in the front of it to escape your backslashes:
在'E:'后面有一个以零结尾的字符(\ 0),将其连接到您的连接字符串时会引起问题.
There is a null-terminating character (\0) after the 'E:' in it causing your issue when it's concatenated to your connection string.
更改此:
string path = System.IO.Path.GetFullPath("E:\09-2013\SalesRep\Openleads.xlsx");
对此:
string path = System.IO.Path.GetFullPath(@"E:\09-2013\SalesRep\Openleads.xlsx");
你应该没事.
这篇关于初始化字符串的格式不符合从索引128开始的规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!