2014-10-04 150 views
0

根据OP here发布的代码(理论上它应该适用于我的格式良好的测试文件),我试图将CSV文件的内容分配给DataGridView:为什么我得到“Microsoft [ODBC Text Driver]”(未知)'不是有效的路径“?

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和构造的值为驱动= {微软文本驱动程序( .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. . . . 

我在做什么错误或失败,在这里做

如果有更好的方法来做到这一点/替代方法,我持开放的态度,太。

回答

1

我知道这是老问题,但我们开始越来越对工作过的文件这个错误。重新启动Excel中后,我们能够再次打开它下一次的错误显示,直到。

似乎就像Excel正在行动起来一样不关闭一些专用锁explained nicely here

你能排除故障吗?

+0

我根本不记得这件事,所以如果我这样做了,它会回退到记忆的黑暗中。 – 2015-07-23 14:49:41

相关问题