本文介绍了C#中的OleDbConnection源变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 OleDbConnection
语句中用 filePath
替换 D:\ temp \ test.xls
.
我可以获取确切的filePath(使用OpenFileDialog,然后可以方便地定位我的.xls文件,而无需再进行硬编码),但是当我将变量 filePath
插入为Style2时,它不起作用.我怎样才能解决这个问题 ?谢谢.
I can get the exactly filePath (with OpenFileDialog, then I can located my .xls file conveniently, no more hardcoded), but when I insert the variable filePath
as Style2, it doesn't work. How can I fix this ? Thanks.
Style1
OleDbConnection dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties=""Excel 8.0;HDR=Yes;""");
Style2
OleDbConnection dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;""");
[已更新]我的部分代码是这样的,
[Updated]Some part of my code as this,
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();
推荐答案
OleDbConnection dbConnection = new OleDbConnection( String.Format( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;""", filePath ) );
这篇关于C#中的OleDbConnection源变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!