C# TableAdapter 缺少插入方法
我正在为我的项目使用 C# VS2010 TableAdapter 向导来与我的数据库进行交互。我已经为此适配器生成了自己的插入、更新和删除命令,因为它使用联接并且无法生成自己的命令。我对此表示同意,但不幸的是,当我创建插入命令时,我无法调用表适配器上的“Insert()”方法。其他方法工作正常。
例如,
`this.joinTableAdapter = new MyTableAdapter();
this.joinTableAdapter.Insert() <-- 不存在。`
据我了解,一旦创建有效的插入命令,此方法应该在表适配器上可见。我对此有误解吗?它适用于我的更新和删除命令。有什么想法我做错了吗?
任何帮助表示赞赏。谢谢大家!
I am using the C# VS2010 TableAdapter Wizard for my project to interface with my db. I have generated my own Insert, Update and Delete commands for this adapter since it uses a join and can not generate its own. I'm okay with that, but unfortunately when I created the Insert command I am not able to call the "Insert()" method on the tableadapter. The other methods are working fine.
For example,
`this.joinTableAdapter = new MyTableAdapter();
this.joinTableAdapter.Insert() <-- does not exist.`
It was my understanding that once you create a valid insert command that this method should become visible on the table adapter. Am I mistaken about this? It is working for my update and delete command. Any ideas what I am doing wrong?
Any help is appreciated. Thanks everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TableAdapterGenerateDbDirectMethods
除了InsertCommand、UpdateCommand 和DeleteCommand 之外,TableAdapter 还使用可直接针对数据库执行的方法创建。可以直接调用这些方法(TableAdapter.Insert、TableAdapter.Update 和 TableAdapter.Delete)来操作数据库中的数据。
如果您不想创建这些直接方法,请将 TableAdapter 的GenerateDbDirectMethods 属性设置为 false(在“属性”窗口中)。添加到 TableAdapter 的其他查询是独立查询 - 它们不会生成这些方法。
您可以在倒数第二页的设计器中找到此选项。
创建将更新直接发送到数据库的方法
注意:这与单击“高级选项”并单击:
生成插入、更新和删除语句
当您选择此选项时,向导将尝试根据“生成 SQL 语句”页上定义的 SELECT 语句生成 INSERT、UPDATE 和 DELETE 语句。
编辑:
当主选择查询涉及多个表时,TableAdapter 无法自动创建插入、更新和删除语句。如果您需要一列或多列相关表,有多种方法,最好的方法取决于您的要求。
InsertUser(param1,param2)
)Update
时,这些命令将从 TableAdapter 隐式触发。 DataRow 的 RowState 确定将触发哪个命令(已添加->插入命令)。对于第三种方法,您可以使用 LINQ-To-DataSet 连接相关表以获取所有需要的列(例如为 GridView 创建数据源)。
TableAdapter GenerateDbDirectMethods
In addition to the InsertCommand, UpdateCommand, and DeleteCommand, TableAdapters are created with methods that can be executed directly against the database. These methods (TableAdapter.Insert, TableAdapter.Update, and TableAdapter.Delete) can be called directly to manipulate data in the database.
If you do not want to create these direct methods, set the TableAdapter's GenerateDbDirectMethods property to false (in the Properties window). Additional queries added to the TableAdapter are standalone queries — they do not generate these methods.
You'll find this option at the designer in the second last page.
Create methods to send updates directly to the database
Note: This is not the same then clicking Advanced Options and click:
Generate Insert, Update, and Delete statements
When you select this option, the wizard will attempt to generate INSERT, UPDATE, and DELETE statements based on the SELECT statement defined on the Generate SQL statements page.
Edit:
The TableAdapter cannot create insert,update and delete statements automatically when more than one table is involved in the main select query. There are several approaches if you need one or more columns of related tables, the best way depends on your requirement.
InsertUser(param1,param2)
)Update
. The DataRow's RowState determines which command will be triggered(f.e. Added->InsertCommand).For the third approach you could use LINQ-To-DataSet to join the related tables to get all needed columns(f.e. to create a DataSource for a GridView).
有第四种处理 JOIN 查询的方法。
对于表适配器的主查询,省略连接。然后,手动将列添加到数据表中,并将其 SourceColumn 设置为您要添加的字段的准确拼写。最后,向包含联接的表适配器添加一个新查询,并调用该查询来填充应用程序中的数据表。
如果这太简短,请告诉我。我会详细说明。
There is a 4th way of working with JOIN queries.
For the table adapter's main query, omit the join. Then, add manually the columns to the data table and set their SourceColumn to the exact spelling of the fields you will add. Finally, add a new query to the table adapter that includes the join and call this query to fill the datatables in your application.
If this is too abbreviated, let me know. I'll elaborate more.
进入数据集,单击表适配器并检查属性“GenerateDBDirectMethods”的设置 - 需要为 true
Go into the dataset click on the tableadaptor and check setting for property "GenerateDBDirectMethods" - Needs to be true