private void Delete_click (object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection ("Database connection string");
Conn. Open ();
SqlDataAdapter daauthors = new SqlDataAdapter ("SELECT * FROM Student", Conn);
DataSet dspubs = new DataSet ("Pubs");
daAuthors.FillSchema (dspubs, SchemaType.Source, "student"); FillSchema the schema and data of the loaded table, with the schema, the table knows which column is its primary key, and the Find method of the Rows collection is available.
daAuthors.Fill (dspubs, "student");
DataTable tblauthors;
tblauthors = dspubs.tables["Student"];
if (MessageBox.Show ("Are you sure you want to delete the line?", "Ask", messageboxbuttons.yesno) = = Dialogresult.yes)
{
DataRow drcurrent;
String Row_zhi = Datagridview1.selectedrows[0]. Cells[0]. Value.tostring ();//Gets the value of the first cell
MessageBox.Show (Row_zhi);
drcurrent = TblAuthors.Rows.Find (Row_zhi);
Drcurrent.delete ();
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder (daauthors); SqlCommandBuilder provides a way to automatically generate single-table commands that coordinate the changes that are made to the DataSet using the associated SQL Server database.
daauthors.update (dsPubs, "student "); Data adapters. Update () method
//messagebox.show (" Database updated successfully! ");
//------- Rebind the DataGridView data source to re-display the-------
daAuthors.Fill (dspubs, "student");
DataTable tblAuthors1;
tblAuthors1 = dspubs.tables["Student"];
Datagridview1.datasource = tblAuthors1;
}
Conn. Close ();
Conn. Dispose ();
MessageBox.Show ("Database connection is closed");
}
Note:
SqlDataAdapter does not automatically generate the Transact-SQL statements required to reconcile changes made to the DataSet (using the associated SQL Server instance). However, if you set the SelectCommand property of SqlDataAdapter, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for single-table updates. SqlCommandBuilder will then generate any other Transact-SQL statements that are not set.
Once the DataAdapter property is set, SqlCommandBuilder registers itself as a listener for the RowUpdating event. You can associate only one SqlDataAdapter with one SqlCommandBuilder object at a time (or vice versa).
To generate an INSERT, UPDATE, or DELETE statement, SqlCommandBuilder automatically uses the SelectCommand property to retrieve the required set of metadata. If you change SelectCommand after retrieving the metadata (for example, after the first update), you should call the RefreshSchema method to update the metadata.
SelectCommand must also return at least one primary key column or a unique column. If nothing is returned, a invalidoperation exception is generated and no command is produced.
SqlCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by SelectCommand. If you modify any of these properties or replace the SelectCommand itself, the user should call RefreshSchema. Otherwise, the InsertCommand, UpdateCommand, and DeleteCommand properties retain their previous values.
If you call Dispose, the SqlCommandBuilder is associated with SqlDataAdapter and the generated command is no longer used.
C#.net Delete DataGridView Select a row and update the database at the same time