To delete a record using a dataset dataset

Source: Internet
Author: User

Using a dataset to delete records is very similar to updating records using Datasets, and the steps for a dataset to delete records are as follows.

Q Create a Connection object.

Q Create a DataAdapter object.

Q Initialize the adapter.

Q Use the Fill method of the data adapter to execute the SELECT command and populate the dataset.

Q Executes the SqlCommandBuilder method to generate the Updatacommand method.

Q Create a DataTable object and specify the table in the corresponding dataset.

Q Create a DataRow object and find the appropriate row to modify.

Q Delete the row using the Delete method.

Q Use the Updata method to update the data.

Before deleting a record, you first need to create the connection, as shown in the sample code below.

String str = "server= ' (local) ';d atabase= ' mytable '; uid= ' sa ';p wd= ' sa '";

SqlConnection con = new SqlConnection (str);

Con. Open ();

String strSQL = "SELECT * from Mynews";

The code above creates a connection to the database and writes SQL query statements to populate the dataset. Populating the DataSet object requires the use of DataAdapter, as shown in the sample code below.

SqlDataAdapter da = new SqlDataAdapter (strSQL, con);

SqlCommandBuilder build = New SqlCommandBuilder (DA);

DataSet ds = new DataSet ();

Da. Fill (ds, "DataTable");

Once written, the DataTable object needs to be created to manipulate the corresponding data in the dataset, with the same code as the update record, as shown in the sample code below.

DataTable TB = ds. Tables["DataTable"];

Tb. PrimaryKey = new datacolumn[] {TB. columns["id"};

DataRow row = tb. Rows.find (3);

Before deleting, you also need to find the appropriate row to specify the row to delete the statement, as shown in the sample code below.

Row. Delete ();

The reader can see that the dataset deletion method differs from the Update method in that it uses only the update () method, and the Delete () method in the deletion.

Note: When you delete a record row by using the Delete method, you can cancel the deletion of the record by calling the RejectChanges method of the DataRow object, and the row's state reverts to unchanged when the record row is deleted using that method.

Once deleted, it is also necessary to maintain consistency between the data in the dataset and the data in the database, as shown in the sample code below.

Da. Update (ds, "DataTable");

Using the Update method allows the data in the dataset to be consistent with the data in the database, which is common in ASP.

To delete a record using a dataset dataset

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.