C#.net Delete DataGridView Select a row and update the database at the same time

Source: Internet
Author: User

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

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.