WinForm DataSet implements data query, save and delete

Source: Internet
Author: User

    Public partial class Form4:form {SqlConnection conn = null;
        SqlDataAdapter ADP = null;
        DataSet ds = null; String constr = "Data source=.;
        Initial Catalog=myschool;user Id=sa;pwd=sa ";
        String sql = "SELECT * from grade";
            Public Form4 () {InitializeComponent ();
            conn = new SqlConnection (CONSTR);
            ADP = new SqlDataAdapter (SQL, conn);
        ds = new DataSet ("Grade");
        #region------------data Query----------------private void Button1_Click (object sender, EventArgs e) {ds.
            Clear (); Adp.
            Fill (ds, "grade"); Datagridview1.datasource = ds.
        Tables["Grade"];  #endregion #region------------data is saved and updated----------------private void button2_click (object sender,
            EventArgs e) {SqlCommandBuilder builder = new SqlCommandBuilder (ADP); Adp. Update (ds, "Grade");
        Updategridview ();  #endregion #region----------Data deletion-----------------private void deletion Toolstripmenuitem_click (object
            sender, EventArgs e) {SqlCommandBuilder builder = new SqlCommandBuilder (ADP);

            int rowIndex = DataGridView1.CurrentRow.Index; Gets the first column content of the selected row             MessageBox.Show (ds. Tables["Grade"]. Rows[rowindex][0].

           ToString ());  ds. Tables["Grade"]. Rows[rowindex].
            Delete (); Adp.
            Update (ds, "grade");
        Updategridview ();
            #endregion #region Update after------------data operation----------------private void Updategridview () { Ds.
            Clear (); Adp.
            Fill (ds, "grade"); Datagridview1.datasource = ds.
        Tables["Grade"]; } #endregion}

SqlCommandBuilder The data in the DataTable is updated back to the SQL database, the corresponding table in the SQL database must have a primary key; SqlDataAdapter does not automatically generate Transact-SQL statements required to reconcile changes between the DataSet and the associated SQL Server instance. However, if you set the SqlDataAdapter SelectCommand property, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for a single table update. SqlCommandBuilder then generates any other Transact-SQL statements that are not set up.
Whenever the DataAdapter property is set, SqlCommandBuilder registers itself as a listener for the RowUpdating event. You can associate only one SqlDataAdapter with a SqlCommandBuilder object (or vice versa) at a time.
To generate an INSERT, UPDATE, or DELETE statement, SqlCommandBuilder automatically uses the SelectCommand property to retrieve the required metadata set. If you change SelectCommand after retrieving the metadata (for example, after the first update), you should call the RefreshSchema method to update the meta data.
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 built.
SqlCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by SelectCommand. If one or more of these attributes are modified, or if the SelectCommand itself is replaced, the user should call RefreshSchema. Otherwise, the InsertCommand, UpdateCommand, and DeleteCommand properties all retain their previous values.
If you call Dispose, the SqlCommandBuilder associated with SqlDataAdapter is lifted, and the generated command is no longer used.

Gets the information in the selected row in the DataGridView

private void Tsmiadd_click (object sender, EventArgs e)
{
MessageBox.Show (this.dgvstudent.selectedrows[0]. Cells[0]. Value.tostring ());
}

You can also use lists to get information

private void Tmsideletecom_click (object sender, EventArgs e)
{
if (This.dgvStudent.SelectedRows.Count > 0)
{
MessageBox.Show (This.dgvstudent.selectedrows[0]. cells["studentno"]. Value);
}
}

The setting corresponding to the setting of the dgvstudent is consistent with the variable name in datapropertyname , as shown in the following figure:


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.