Insert method of TableAdapter not working?

末鹿安然 提交于 2020-01-20 08:06:19

问题


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???


回答1:


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.




回答2:


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(..)


来源:https://stackoverflow.com/questions/4550437/insert-method-of-tableadapter-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!