简体   繁体   English

如何以及在何处修改DataAdapter中自动生成的查询?

[英]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.

相关问题 当我使用SELECT查询实例化DataAdapter时,如何为DataAdapter.Update设置命令超时? - How do I set the command timeout for DataAdapter.Update when I instantiate the DataAdapter using a SELECT query? 不允许使用带有WHERE子句的SqlDependency查询。 如何修改它才有效? - SqlDependency query with WHERE clause not allowed. How can I modify it to be valid? 如何提高 dataadapter.fill 的性能? - How can I improve performance of dataadapter.fill? 如何在我的 Linq-To-SQL 查询中为可导航 Object 添加动态生成的 Where 表达式? - How Can I Add a Dynamically Generated Where Expression for a Navigable Object in My Linq-To-SQL Query? ElasticSearch-如何从插入查询中获取自动生成的ID - ElasticSearch - how to get the auto generated id from an insert query 如何从 reactJS 或 C# 中的 JSON 获取自动生成的 ID - How can i get auto generated id from JSON in reactJS or C# 如何忽略在自动生成的 WSDL 中序列化的属性? - How can I ignore a property from being serialized in auto generated WSDL? 如何修改自动生成的实体框架DbContext属性? - How do I modify an auto-generated Entity Framework DbContext Property? 如何在序列化中更改属性名称(自动生成类的名称)? - How can I change properties' names (of auto generated classes) in serialization? 如何保存对自动生成文件的更改? - How can I preserve changes to a auto generated file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM