Difference between DataTable. Rows. Remove (row) and DataTable. Rows [I]. Delete ()

Source: Internet
Author: User

We are working on ADO today. NET adapter automatically triggers the database update function for data, and finds a very strange problem. Originally, data operations were performed by their own code to complete the update, and this method was never involved, so it was a problem.

 

 1 SQLiteDataAdapter dataAdpater = new SQLiteDataAdapter("Select cid,sd from VC", conn);
2 dataAdpater.DeleteCommand = new SQLiteCommand("delete From VC Where cid = @cid", conn);
3 dataAdpater.DeleteCommand.Parameters.Add("@cid", DbType.Int32, 4, "cid");
4 dataAdpater.DeleteCommand.Parameters["@cid"].SourceVersion = DataRowVersion.Original;
5
6 DataTable categoryTable = new DataTable();
7 dataAdpater.Fill(categoryTable);
8 categoryTable.PrimaryKey = new DataColumn[]{categoryTable.Columns["cid"]};
9
10 DataRow dr = categoryTable.Rows.Find(213213);
11 categoryTable.Rows.Remove(dr);
12 dataAdpater.Update(categoryTable);

Run the above Code and find that the physical database has not been deleted. It is strange that the 213213 record corresponding to the last debug statement does not exist. Why has the database not been deleted, later, I found that DataRow has a Delete function in MSDN. I will try it immediately and Delete it successfully.

1 DataRow dr = categoryTable.Rows.Find(213213);
2 dr.Delete();
3 dataAdpater.Update(categoryTable);

The query on MSDN is explained as follows:

DataTable. Rows. Remove (row): Calling Remove is the same as calling Delete and then calling AcceptChanges.

DataTable. Rows [I]. Delete (): A deleted row can be undeleted by invoking RejectChanges.

 

In this way, it is clear that all record sets in the DataTable have their own modification flag, proving that each record is "add", "delete", "modify", and all adpater. during the Update process, different cmd commands will be called to synchronize the final data. Ten years ago, my colleague used XML to return the dataset. This method was used for local offline editing and then submitted to the database at one time, ha, my colleague is indeed awesome!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.