我正在尝试使用oledb证明程序读取csv文件,在堆栈溢出中尝试了很多尝试以找到解决方案,但是直到我卡在同一个地方。请检查以下代码,我做错了什么或我错过了什么。
OleDbConnectionStringBuilder Builder = new
OleDbConnectionStringBuilder();
if (filetype.Equals("csv"))
{
type = ".csv";
Builder.Provider = "Microsoft.Jet.OLEDB.4.0";
Builder.Add("Extended Properties",
string.Format("Text;HDR=YES;FMT=Delimited;", "No"));
}
//folderpath ="D://TestFolder";
Builder.DataSource = folderpath ;
var dt = new DataTable();
string query = string.Empty;
if (type.Equals(".xlsx"))
{
query = "SELECT top 1 * FROM [Sheet1$]";
}
else if (type.Equals(".csv"))
{
query = "SELECT top 1 * FROM " + entityItem;
}
using (OleDbConnection cn = new OleDbConnection {
ConnectionString = Builder.ConnectionString })
{
using (OleDbCommand cmd = new OleDbCommand { CommandText
= query, Connection = cn })
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Open();
OleDbDataReader dr = cmd.ExecuteReader();//Here i m getting error
dt.Load(dr);
}
}
//错误= System.Data.OleDb.OleDbException:'Microsoft Jet数据库引擎找不到对象'AccountTest.txt'。确保该对象存在,并且正确拼写了它的名称和路径名。'
问题是它是csv文件,然后为什么错误显示了txt文件
下面是我的csv文件(AccountTest.csv)
帐户ID,帐户名称,电话,帐户编号
1,nandeshwar,4324353,5325353