为什么我得到“;Microsoft][ODBC文本驱动程序]';(未知)';不是有效路径”;

本文关键字:未知 路径 有效 文本 Microsoft 为什么 ODBC 驱动程序 | 更新日期: 2023-09-27 18:25:30

根据OP在这里发布的代码(理论上,这应该适用于我所拥有的格式良好的测试文件),我正在尝试将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;扩展名=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.  . . .

我在这里做错了什么或没做什么?

如果有更好的方法来做这个/替代方法,我也持开放态度。

为什么我得到“;Microsoft][ODBC文本驱动程序]';(未知)';不是有效路径”;

我知道这是个老问题,但我们开始在以前工作的文件上遇到这个错误。重新启动Excel后,我们可以再次打开它,直到下次出现错误为止。

Excel似乎在发挥作用,没有像这里很好地解释的那样关闭一些独占锁。

你能排除故障吗?