我正在尝试将 excel 文件连接到我的 C# 代码。但是,代码会引发异常:"'ServerVersion' 抛出类型为'System.InvalidOperationException'的异常"?我能做些什么来修复它?
OleDbConnection con = new OleDbConnection();
con.ConnectionString= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource='D:/.Net devlop/ADO_QuickKart Application_14Nov16_1807/QuikkartDB.xlsx';Extended Properties='Excel 12.0 Xml;HDR=YES'";
您的路径包含 ' ,这导致了问题。
例:
Data Source=c:myFoldermyExcel2007file.xlsx;
\ 和 '.
string filePath = "D:\.Net devlop\ADO_QuickKart Application_14Nov16_1807\QuikkartDB.xlsx";
string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={filePath};Extended Properties="Excel 12.0;HDR=YES;"";
或者使用 OleDb.OleDbConnectionStringBuilder
OleDb.OleDbConnectionStringBuilder Builder = new OleDb.OleDbConnectionStringBuilder();
Builder.DataSource = "test.xlsx";
Builder.Provider = "Microsoft.ACE.OLEDB.12.0";
Builder.Add("Extended Properties", "Excel 12.0;HDR=Yes;IMEX=1");
Console.WriteLine(Builder.ConnectionString);