DataAdapter.Update提供了一个额外的列 [英] DataAdapter.Update giving an extra column
问题描述
大家好.
我正在使用ODBC Microsoft文本驱动程序将csv文件加载到要在datagridview中显示的数据集中,目前DGV中没有任何列.我可以成功加载文件,但是遇到2个问题:
a)2列可以很好地加载,但是第三个"noname"列也可以加载-为什么会发生这种情况?
b)当我使用DataAdapter进行更新时,出现以下异常:
"ERROR [HYS22] [Microsoft][ODBC Text Driver] The INSERT INTO statement contains the following unknown field name: ''NoName''. Make sure you have typed the name correctly, and try the operation again."
有人知道为什么会这样吗?
非常感谢,
查尔斯
***以下代码***
Hello all.
I''m using the ODBC Microsoft Text Driver to load a csv file into the dataset to be displayed in the datagridview, the DGV has no columns in at present. I can load the file successfully but I encounter 2 problems:
a) The 2 columns load fine but a third ''noname'' column is also loaded - why does this happen?
b) When I do an update using a DataAdapter, I get this exception:
"ERROR [HYS22] [Microsoft][ODBC Text Driver] The INSERT INTO statement contains the following unknown field name: ''NoName''. Make sure you have typed the name correctly, and try the operation again."
Does anybody know why this occurs?
Many thanks,
Charles
*** Code below ***
void RefreshData()
{
OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\;");
OdbcCommand comm = new OdbcCommand("Select * FROM test.csv", conn);
OdbcDataAdapter adapter = new OdbcDataAdapter(comm);
DataSet ds = new DataSet();
adapter.Fill(ds);
// ds.Tables[0].Columns.Remove("noname");
dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\;");
string qry = "Select * FROM test.csv";
OdbcDataAdapter da = new OdbcDataAdapter();
da.SelectCommand = new OdbcCommand(qry, conn);
OdbcCommandBuilder cb = new OdbcCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
//ds.Tables[0].Columns.Remove("noname");
DataTable dt = ds.Tables[0];
// Add a row
DataRow newRow = dt.NewRow();
newRow[0] = this.textBox1.Text;
newRow[1] = int.Parse(this.textBox2.Text); ;
dt.Rows.Add(newRow);
da.Update(ds.Tables[0]);
conn.Close();
this.Refresh();
*** CSV数据***
empName,工资,
查尔斯,4324343,
andrew,31343970,
freddy,998788966,
loop,8878743,
*** csv Data ***
empName,salary,
charles,4324343,
andrew,31343970,
freddy,998788966,
loop,8878743,
推荐答案
在涉及SQL查询时,您永远不要使用SELECT * FROM test.csv
,而是要按名称命名要提取的列.因此,如果您这样做
When it comes to SQL queries you should never useSELECT * FROM test.csv
, rather name the columns you want to extract by name. Thus if you do this
void RefreshData()
{
OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\;");
//Get specific columns
OdbcCommand comm = new OdbcCommand("Select empName, salary FROM test.csv", conn);
OdbcDataAdapter adapter = new OdbcDataAdapter(comm);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
那你应该没事的.
希望这对您有帮助
then you should be OK.
Hope this helps
这是您修改后的更新语句
here is your modifed update statement
da.Update(ds.Tables[0].DefaultView.ToTable(true, new string[] { "empName", "salary" }));
试试这个....
try with this....
这篇关于DataAdapter.Update提供了一个额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!