AcceptChanges and Updates once more

本文探讨了在数据库中批量删除记录的高效方法,强调了使用TRUNCATE TABLE相较于逐条DELETE的优势,并提供了理解数据库Adapter行为的关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

So the following question was posted to a forum I frequent and it serves a good point of illustration:

"I wont to clear all the data from tables in the database. I have studentiDataset, and i tried this code:
Form2.StudentiDataSet.Clear()
Form2.StudentiDataSet.AcceptChanges()
I also tried to add after this code tableadapter update method for all tables but it didn't work.
How can i doo that?
Thanks!"

 

Remember the basic behavior of Adapters. When the Update method is called, it loops through each row and checks theRowState. Depending on what it finds, a different action is taken. 

  • The row's RowState is Deleted:
    • The adapter looks for a valid Delete command and executes it.
    • It calls AcceptChanges on the row unless AcceptChangesOnUpdate is set to false
    • If no Delete command is present, it throws an exception
  • The row's RowState is Modified:
    • The adapter looks for a valid Update command and executes it
    • It calls AcceptChanges on the row unless AcceptChangesOnUpdate is set to false
    • If no Update command is present, it throws an exception
  • The row's RowState is Added:
    • The adapter looks for a valid Insert command and executes it
    • It calls AcceptChanges on the row unless AcceptChangesOnUpdate is set to false
    • If not Insert command is present, it throws an exception
  • The row's RowState is Unchanged:
    • Nothing happens

Ok pay close attent to the last of these choices.  So what happens when you callAcceptChanges?  Well, you can call it on a Row, a DataTable or a DataSet.  Whenever you call it on aDataRow, the RowState goes back to Unchanged.  When you do it to a DataTable, all the rows in the Table are set back to Unchanged. And when done on aDataSet, it sets all the rows in all the tables back to Unchanged.

So what happens if you call Update after AcceptChanges? Unless something is changed in between them, it has a similar effect to calling Thread.Sleep(10); because it basically does nothing.

Now, specifically to this question. AcceptChanges is ensuring that none of the deleted rows are every sent back to the database.  However unless you had a specific reason (like a transaction) to do so, then this is probably one of the least efficient ways to delete all the rows for a table.  "TRUNCATE TABLE" is the most efficient option and is appropriate unless you need the changes logged.  Otherwise, a single "DELETE FROM Whatever" should work.  Imagine having 10,000 rows.  Truncate or delete do it in one action as opposed to 10,000 of them so unless there's a compelling reason to do otherwise, this should be avoided.

 

原文地址:http://msmvps.com/blogs/williamryan/archive/2007/02/09/acceptchanges-and-updates-once-more.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值