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