How the SqlCommandBuilder class constructs T-SQL statements

Source: Internet
Author: User
Tags what sql

This blog by default you read "RowState attribute in DataTable" this blog.

When using SqlCommandBuilder it's easy to create a SqlCommandBuilder object and then set its DataAdapter property, but actually, the SqlCommandBuilder object does a lot of things for us, That is, building T-SQL commands to enable our database to synchronize with our operations, and when invoking the update () method of the SqlDataAdapter instance, the SqlCommandBuilder instance iterates over the rows we have modified or added. Build different T-SQL commands based on the RowState of the row.

Let's look at what SQL commands the SqlCommandBuilder instance has built:

The usual code came first.

--build a database and build a tableCreate Databasestudent; Usestudent;Create TableStudent (snamevarchar(Ten) not NULL, Snoint  not NULL, Sageint  not NULL, Ssexvarchar(2) not NULL);Alter TableStudentAdd constraintPk_snoPrimary Key(Sno),constraintCk_ssexCheck(Ssex= 'male' orSsex= 'female'),        constraintCk_sageCheck(Sage> 8  andSage<  +)Insert  intoStudentValues('Zhang San',103, at,'male');Insert  intoStudentValues('John Doe',104, -,'male');Insert  intoStudentValues('Harry', the, -,'male');Insert  intoStudentValues('Zhao Liu',106, -,'male');Insert  intoStudentValues('Zhu Qi',107, -,'male');Select *  fromstudent;DeleteStudent

C # code:

 Public Static voidAdapterandsqlcommand () {//First step: Get Database configuration informationString connstr = configurationmanager.connectionstrings["ConnStr"].            ToString (); //Step Two: Build SqlCommand query StatementsSqlCommand Command =NewSqlCommand ("select * from student;"); Command. Connection=NewSqlConnection (CONNSTR); //Step Three: Create SqlDataAdapterSqlDataAdapter adapter =NewSqlDataAdapter (command); //Fourth Step: Create Datasets and DataTableDataSet DataSet =NewDataSet (); DataTable DataTable=NewDataTable (); //Fifth Step: Populating the Dataadapter.            Fill (dataTable);            DATASET.TABLES.ADD (dataTable); //change the name in the first row of data to small red, and change its gender to femaledatatable.rows[0]["sname"] ="Little Red"; datatable.rows[0]["Ssex"] ="female"; //Delete the second row of datadatatable.rows[1].            Delete (); //build a new row and add it to the tableDATATABLE.ROWS.ADD (New Object[] {"Xiao Ming",108, -,"male" }); //creates a SqlCommandBuilder object and binds a SqlDataAdapter objectSqlCommandBuilder SCB =NewSqlCommandBuilder (adapter); //Print output SqlCommandBuilder object additions and Deletions change SQL command statementConsole.WriteLine ("Insert command for SqlCommandBuilder instance:"+SCB. GetInsertCommand ().            CommandText); Console.WriteLine ("Delete command for SqlCommandBuilder instance:"+SCB. GetDeleteCommand ().            CommandText); Console.WriteLine ("Update command for the SqlCommandBuilder instance:"+SCB. GetUpdateCommand ().            CommandText); //synchronizing rows with changes to the databaseadapter.            Update (Datatable.getchanges ()); //Save Changesdatatable.acceptchanges (); //The following is a traversal of the data in the output DataTable            foreach(DataTable tableinchdataset.tables) {foreach(DataRow rowinchtable. Rows) {Console.WriteLine (row[0] +", "+ row[1] +", "+ row[2] +", "+ row[3]); }            }        }

The result of running the printout is as follows: In-memory data changes as expected

What about the database?

is also normal and has been synchronized to the database. One has been modified, one has been deleted, and one has been added.

But we look at the SQL command printed by the console, the commands are right, but how are local variables, and when are these local variables replaced?

How the SqlCommandBuilder class constructs T-SQL statements

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.