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