The combination of "dataset" and "SqlDataAdapter" of the learning of ADO to realize the "increase", "delete" and "change" of the data

Source: Internet
Author: User

Prepare: Create a table in the database: Table name: Picinfos

Table structure:

Table data:

The link to the database is not much to say. Go directly to today's topic: Using Datasets and SqlDataAdapter to manipulate data in a database:

First, view the data:

 Public voidDosqldata () {using(SqlConnection sqlconn =NewSqlConnection (SQLSTR)) {                stringstr ="SELECT * from Picinfos";//SQL query StatementsSqlDataAdapter SDA =NewSqlDataAdapter (str, sqlconn); DataSet DS=NewDataSet ();//Create a DataSet collectionSda. Fill (DS);//to store the results of a query in the DataSet collectionGridview1.datasource = ds;//Specify the data sourceGridview1.databind ();//binding Data            }        }

The results are as follows:

Second, add data:

 Public voidDosqldata () {using(SqlConnection sqlconn =NewSqlConnection (SQLSTR)) {                stringstr ="SELECT * from Picinfos";//SQL query StatementsSqlDataAdapter SDA =NewSqlDataAdapter (str, sqlconn); DataSet DS=NewDataSet ();//Create a DataSet collectionSda. Fill (DS);//to store the results of a query in the DataSet collection//use a dataset to add data to the database and update the data with SqlDataAdapterSqlCommandBuilder Cmdbuilder =NewSqlCommandBuilder (SDA);//Key points//add a row, instantiate a row object, and note that the row is created with NewRowDataRow dr = ds. tables[0]. NewRow ();//When you create a new line//the added datadr["name"] ="Tom"; dr["content"] ="Tom, is that Tom?"; dr["type"] ="Data"; dr[" Price"] ="0"; dr["Picurl"] ="Image\\6.png"; Ds. tables[0].                Rows.Add (DR); if(ds. HasChanges ())//If the DS collection has changed{SDA.    Update (DS); //Update Data} gridview1.datasource= ds;//Specify the data sourceGridview1.databind ();//binding Data            }        }

The results are as follows:

Third, modify the data:

 Public voidDosqldata () {using(SqlConnection sqlconn =NewSqlConnection (SQLSTR)) {                stringstr ="SELECT * from Picinfos";//SQL query StatementsSqlDataAdapter SDA =NewSqlDataAdapter (str, sqlconn); DataSet DS=NewDataSet ();//Create a DataSet collectionSda. Fill (DS);//to store the results of a query in the DataSet collectionSqlCommandBuilder ACB=NewSqlCommandBuilder (SDA); Ds. tables[0]. rows[ A]["Content"] ="I changed the "content" of the 12th row of data. New Data"; if(ds. HasChanges ()) {SDA.                    Update (DS); Ds.                AcceptChanges (); } Gridview1.datasource= ds;//Specify the data sourceGridview1.databind ();//binding Data            }        }

The result is as follows:

IV. Delete data:

 Public voidDosqldata () {using(SqlConnection sqlconn =NewSqlConnection (SQLSTR)) {                stringstr ="SELECT * from Picinfos";//SQL query StatementsSqlDataAdapter SDA =NewSqlDataAdapter (str, sqlconn); DataSet DS=NewDataSet ();//Create a DataSet collectionSda. Fill (DS);//to store the results of a query in the DataSet collectionSqlCommandBuilder ACB=NewSqlCommandBuilder (SDA); Ds. tables[0]. rows[ -]. Delete ();//Delete data from row 13th                if(ds. HasChanges ()) {SDA.                    Update (DS); Ds.                AcceptChanges (); } Gridview1.datasource= ds;//Specify the data sourceGridview1.databind ();//binding Data            }        }

The results are as follows:

Note: DS. Tables[0]. ROWS[13] has a number of rows starting from 0.

Write a blog, convenient and convenient for others!

The combination of "dataset" and "SqlDataAdapter" of the learning of ADO to realize the "increase", "delete" and "change" of the data

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.