TableAdapter 的插入方法不起作用?

发布于 2024-10-09 04:41:22 字数 377 浏览 0 评论 0原文

我在我的 C# 项目中使用 ADO.NET。在我的表单中,我从 VS2010 的工具箱中添加了一个 SourceBinding 元素。我设置了与数据集表的连接。它会自动为 my.createDataAdapter 创建一个 DataAdapter。

我想插入一条记录,所以我调用DataAdapter的Insert()方法。但是当我查看数据库数据时,它没有任何新记录...

orderID = this.orderTableAdapter.Insert("", "", 
                (int)OrderStatus.IN_CONSTRUCTION, DateTime.Now);

或者我需要使用 SqlCommand 手动插入它???

I'm using ADO.NET in my C# project. In my form I added a SourceBinding element from my toolbox in VS2010. I set the connection to the table of my dataset. It creates a DataAdapter automaticly for my.

I want to insert a record, so I call the Insert() method of the DataAdapter. But when I view my database data, it doesn't have any new records...

orderID = this.orderTableAdapter.Insert("", "", 
                (int)OrderStatus.IN_CONSTRUCTION, DateTime.Now);

Or do I need to insert it manually with the SqlCommand???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚我心虐我身 2024-10-16 04:41:22

表适配器设计为与数据集一起使用,以帮助您使用该数据集将数据传入和传出数据库。

想法是,您可以使用 Dataset.NewYourTableNameRow() 为数据集创建一个新行,然后填充其字段,然后调用 DataSet.AddYourTableNameRow(row) 来放置它在数据集中。

现在您可以 orderTableAdapter.update(DataSet) 将新行传输到数据库中。

要删除或更新一行,您需要首先将其选择到数据集中,对对象执行更改,然后在适当的表适配器上调用 .update(ds) 将其发送回。

The table adapters are designed to be used with a dataset, to help you get data in and out of the DB using this dataset.

Idea is that you can use the Dataset.NewYourTableNameRow() to create a new row for your dataset, then populate its fields and then call DataSet.AddYourTableNameRow(row) to put it in the dataset.

Now you can orderTableAdapter.update(DataSet) to transmit that new row into the database.

To delete or update a row, you would select it first into your dataset, perform a change to the object, then call the .update(ds) on the appropriate table adapter to send it back down.

归途 2024-10-16 04:41:22

我也很难弄清楚这一点。

您需要在

对我有用的 TableAdapter.Insert(...) 之后调用 DataSet.AcceptChanges()

所以步骤是:

  1. 使用 Visual 创建绑定源、表适配器和数据集
    工作室。
  2. 自动添加
  3. TableAdapter.Fill(..) // 这应该由 vs. TableAdapter.Insert(..)
  4. DataSet.AcceptChanges()
  5. TableAdapter.Update(..)

I too was having difficulty figuring this out.

You need to call DataSet.AcceptChanges() after the TableAdapter.Insert(...)

That worked for me.

So the steps are:

  1. Create your bindingsource, tableadapter and dataset using visual
    studio.
  2. TableAdapter.Fill(..) // this should automatically be added by vs.
  3. TableAdapter.Insert(..)
  4. DataSet.AcceptChanges()
  5. TableAdapter.Update(..)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文