【发布时间】:2014-10-04 21:58:15
【问题描述】:
根据 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. . . .
我在这里做错了什么或没有做错什么?
如果有更好的方法/替代方法,我也愿意。
【问题讨论】:
标签: c# csv datagridview odbc