请协助。 在我的代码中收到以下错误;
constr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DAKTARI\Desktop\smarttable.xls;Extended Properties=''Excel 12.0 Xml;HDR=YES;''", FilePath);
第 26 行:Econ = new OleDbConnection(constr);
请协助
您使用了string.Format
,但从未在该字符串中的任何位置使用过FilePath
变量。您需要使用{0}
设置它而不是路径 - 因为它是位置 0 的参数(它是您情况下的第一个也是唯一一个参数)。
但是,该错误是由您在Extended Properties
部分中使用双''
引起的,您只需要一个'
。
请尝试以下操作:
constr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;'", FilePath);
关于string.Format
的更多解释:
当你使用string.Format
时,你传递给字符串参数,用{0}
、{1}
、{2}
等替换。
所以,例如,
string.Format("Hello {0}, how are you {1}?", "John", "today");
将生成一个字符串:
"Hello John, how are you today?"