根据OP here发布的代码(理论上我应该使用格式良好的测试文件对我有用),我正在尝试将CSV文件的内容分配给一个DataGridView:
string conStr = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + selectedFile + ";Extensions=csv,txt";
OdbcConnection conn = new OdbcConnection(conStr);
OdbcDataAdapter da = new OdbcDataAdapter("Select * from [" + Path.GetFileName(selectedFile) + "]", conn);
DataTable dt = new DataTable(selectedFile);
da.Fill(dt);
dataGridViewFileContents.DataSource = dt;
da.Dispose();
conn.Close();
conn.Dispose();
在我的测试场景中运行时,selectedFile的值为 C:\ PersianUtil \ persianUtilOutput.txt ,conStr的值为 Driver = {Microsoft Text Driver( .txt; .csv)}; Dbq = C:\ PersianUtil \ persianUtilOutput.txt; Extensions = csv,txt
但是在“ da.Fill(dt); ”这一行我得到了:
System.Data.Odbc.OdbcException was unhandled
HResult=-2146232009
Message=ERROR [HY024] [Microsoft][ODBC Text Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
ERROR [01000] [Microsoft][ODBC Text Driver]General Warning Unable to open registry key 'Temporary
(volatile) Jet DSN for process 0x26d4 Thread 0x11d8 DBC 0x8649fd4 Text'.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [01000] [Microsoft][ODBC Text Driver]General Warning Unable to open registry key 'Temporary
(volatile) Jet DSN for process 0x26d4 Thread 0x11d8 DBC 0x8649fd4 Text'.
ERROR [HY024] [Microsoft][ODBC Text Driver] '(unknown)' is not a valid path. . . .
我做错了什么或没有做到这一点?
如果有更好的方法来做这个/替代方法,我也会对此持开放态度。
答案 0 :(得分:1)
我知道这是一个老问题,但我们开始在之前有效的文件上收到此错误。重新启动Excel后,我们再次能够打开它,直到下次出现错误。
似乎Excel正在执行并且没有将某些独占锁关闭为explained nicely here。
你能解决它吗?