[英]How and where can I modify the auto generated query from DataAdapter?
I am using a DataGridView and I wanted to update my table from the grid. 我正在使用DataGridView,我想从网格更新我的表。 I get the error "Dynamic SQL generation is not supported against multiple base tables" when using the DataAdapter.Update(dataTable). 使用DataAdapter.Update(dataTable)时,我收到错误“不支持多个基表的动态SQL生成”。 The reason is because I have filled my dataTable by joining 2 tables. 原因是我通过加入2个表填充了我的dataTable。
This is the function I am using to update my database from the grid. 这是我用来从网格更新我的数据库的功能。
private void button2_Click(object sender, EventArgs e)
{
dataGridView2.EndEdit();
//da.Update(dataTable);
OleDbCommand com = new OleDbCommand();
com.Connection = connection;
com.CommandText = "update Name_Corpus2 set EngWord = @EngWord where ID = @ID";
com.Parameters.Add("@ID", OleDbType.Integer, 32, "ID");
com.Parameters.Add("@EngWord", OleDbType.VarChar, 64, "EngWord");
da.UpdateCommand = com;
da.Update(dataTable);
MessageBox.Show("Updated");
bind(classification, language);
}
I understand that I would have to create my own query for updating the table from the grid. 我知道我必须创建自己的查询来从网格更新表。 I would like to know how and where would I have to enter the code to auto generate my update query. 我想知道如何以及在何处输入代码以自动生成更新查询。
Here is my updated function to auto generate the update query, by switching from OleDB commands to SqlCommand and adding the commandtext and parameters for it. 这是我更新的自动生成更新查询的函数,通过从OleDB命令切换到SqlCommand并为其添加命令文本和参数。 Thank you @Steve 谢谢@Steve
private void button2_Click(object sender, EventArgs e)
{
dataGridView2.EndEdit();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.Parameters.Add("@EngWord", SqlDbType.NVarChar, 256, "EngWord");
com.Parameters.Add("@ID", SqlDbType.Int, 32, "ID");
com.CommandText = "update Name_Corpus2 set EngWord = @EngWord where ID = @ID";
da.UpdateCommand = com;
da.Update(dataTable);
MessageBox.Show("Updated");
bind(classification, language);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.