【发布时间】:2011-09-19 05:26:23
【问题描述】:
这是我将 CSV 文件加载到我的 WinForms 应用程序中的 DataGridView 的代码:
private void loadCSV(string path)
{
if (!File.Exists(path))
{
MessageBox.Show(this, "File does not exist:\r\n" + path, "No File", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
try
{
string conStr = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:;Extensions=csv,txt";
OdbcConnection conn = new OdbcConnection(conStr);
OdbcDataAdapter da = new OdbcDataAdapter("Select * from " + path, conn);
dt = new DataTable(path);
da.Fill(dt);
this.path = path;
dataGridView.DataSource = dt;
da.Dispose();
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(this, "There was an error loading the CSV file:\r\n" + ex.Message, "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
它适用于几乎所有有效的 CSV 文件,除了包含某些字符的文件夹中的文件。
例如它适用于
- C:\Users\Public\Desktop\MyCSV.csv
但不是为了
- C:\Users\Public\Desktop\My Folder\MyCSV.csv
有人知道我该如何解决吗?我想我需要以某种方式增加 conStr。
【问题讨论】:
-
您所说的“不工作”是什么意思,对于哪些字符?你能发布错误吗?
-
42S02 "Microsoft Jet 数据库引擎找不到对象"
-
先生您能帮忙吗?如果我可以从我的 datagriview 创建一个 csv 文件?
标签: c# csv datagridview odbc