Microsoft Jet数据库引擎无法打开文件c#

时间:2014-12-05 16:30:03

标签: c# database excel csv jet

您好我在使用Jet数据库引擎读取CSV文件时遇到了一些问题。我收到这条消息:

The Microsoft Jet database engine cannot open the file 'C:\Users\mikec\Desktop\'.  It is already opened exclusively by another user, or you need permission to view its data.

我有权访问此文件夹和文件。我也试过放入我的Visual Studio项目,仍然得到同样的错误。这是我的代码

string path = @"C:\Users\mikec\Desktop\Book1.csv";

string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);

//create the "database" connection string 
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"Excel 8.0;HDR=Yes;FMT=Delimited\"";

//create the database query
string query = "SELECT * FROM " + file;

//create a DataTable to hold the query results
DataTable dTable = new DataTable();
OleDbConnection con = new OleDbConnection(connString);

//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, con);

try
{
    //fill the DataTable
    dAdapter.Fill(dTable);

}
catch (InvalidOperationException e)
{

}

我很困惑为什么会发生这种情况我已经浏览了互联网,无法找到解决方案。

如果那里的任何人能指出我正确的方向,那将是很棒的。

感谢。

0 个答案:

没有答案