如何D:\temp\test.xls用filePathinOleDbConnection语句替换。
D:\temp\test.xls
filePath
OleDbConnection
我可以获取确切的filePath(使用OpenFileDialog,然后可以方便地定位.xls文件,而无需再进行硬编码了),但是当我将变量插入filePath为Style2时,它将不起作用。我怎样才能解决这个问题?谢谢。
样式1
OleDbConnection dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties=""Excel 8.0;HDR=Yes;""");
样式2
OleDbConnection dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;""");
[更新]我的部分代码是这样的,
DataTable fooData = new DataTable(); OleDbConnection dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;"""); dbConnection.Open (); try { OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM [maleSheet$]", dbConnection); OleDbDataReader dbReader = dbCommand.ExecuteReader(); int RankIndex = dbReader.GetOrdinal("Rank"); while (dbReader.Read()) { string rank = dbReader.GetValue(RankIndex).ToString(); ////.... } }
错误如下 OleDbDataReader dbReader = dbCommand.ExecuteReader();
OleDbDataReader dbReader = dbCommand.ExecuteReader();
System.Data.dll中发生了类型为’System.Data.OleDb.OleDbException’的未处理异常
OleDbConnection dbConnection = new OleDbConnection( String.Format( @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=”“Excel 8.0;HDR=Yes;”“”, filePath ) );