Ado. NET updating a database using the dataset class

Source: Internet
Author: User

1. Data is first populated from the database into the dataset class, and tables in the class and tables in the database are mapped to each other.

2. Modify the tables in the DataSet class (INSERT, UPDATE, DELETE, etc.)

3. Sync to database: Use SqlDataAdapter instance name. Update (DataSet instance name, DataSet, and table name mapped in the database), it must be combined with SqlCommandBuilder

SqlCommandBuilder: Automatic generation of single-table commands to reconcile changes made to the DataSet with changes to the associated SQL Server database, which means generating appropriate SQL statements for database execution to update the database

Update () Method: Executes the command that was just generated automatically

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data;usingSystem.Data.SqlClient;namespaceconsoleapplication2{classProgram {Static voidMain (string[] args) {            stringSource ="server= (local) \\sqlexpress;integrated security=true;database=student"; SqlConnection Con=NewSqlConnection (source); Con.            Open (); if(Con. state = =ConnectionState.Open) Console.WriteLine ("The database is connected! "); SqlDataAdapter SDA=NewSqlDataAdapter ();//Defining Data AdaptersDataSet ds =NewDataSet (); //querying the database through a data adapter            string Select="SELECT * FROM class"; SqlCommand COM1=NewSqlCommand (Select, con);//define a query commandSda. SelectCommand = com1;//use SDA. SelectCommand Execute this query command (select a record in the data source)Sda. Fill (DS,"result");//populate the query results into the dataset class and name it Selectresult//Show Query Results            foreach(DataRow xinchDs. tables["result"]. Rows) Console.WriteLine ("name:{0} id:{1}", x[0], x[1]); //add a new row to the DataTable in the dataset (the first way)://using the NewRow () method, return a blank line, populate the data, and then add it to the Rows collectionDataRow r = ds. tables["result"].            NewRow (); r["name"] ="Data Structure"; r["ID"] =3; Ds. tables["result"]. Rows.Add (r);//add a new row to the DataTable in the dataset (the second way)://passes a set of initialized arrays to the Row.add () methodDs. tables["result"]. Rows.Add (New Object[] {"Java",4}); //Delete a specific line             for(intI=0; I<ds. tables["result"]. rows.count;i++)            {                if(ds. tables["result"]. rows[i]["name"]. ToString (). Trim () = ="C language")//trim (): Need to remove leading blank string and trailing white string that stores data in rows                {                    //the difference between remove () and delete (): When remove removes a row, the rows following the row are all automatically moved forward, and delete does not move forward, but using delete cannot be updated to the database because the deletion code for the SQL cannot be generated //ds. tables["Result"]. Rows.remove (ds. tables["Result"].                    Rows[i]); //i--;Ds. tables["result"]. Rows[i]. Delete ();//another way to remove a row}} SqlCommandBuilder SCB=NewSqlCommandBuilder (SDA);//automatically generate single-table commands to reconcile changes made to the DataSet with changes to the associated SQL Server database, which means generating appropriate SQL statements for database execution to update the databaseSda. Update (DS,"result");//and SqlCommandBuilder must be used in combination to execute the command that was just generated, "result" is a dataset and a table that maps to each other in the database, meaning that the changes made in the result table are synchronized to the database source table            foreach(DataRow xinchDs. tables["result"]. Rows) Console.WriteLine ("name:{0} id:{1}", x[0], x[1]); }    }}

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.