C # and database Access Technology Summary (15) DataAdapter Object code example

Source: Internet
Author: User

DataAdapter Object code example

The following code shows how to populate a DataSet object with a DataAdapter object.

Private Static stringstrconnect="data source=localhost;Uid=sa;pwd=aspent;database=logindb"stringSqlstr="SELECT * from USER";//using constructors, create DataAdapterSqlDataAdapter da=NewSqlDataAdapter (Sqlstr, strconnect);//Create a DataSetDataSet ds=NewDataSet ();//fill, the first parameter is the DataSet object to be populated, and the second parameter is the datatabble that fills the datasetDa. Fill (DS,"USER");

The above code uses the Dataapater object to populate the DataSet object with the following steps.

(1) Create a SqlDataAdapter object based on the connection string and the SQL statement.

Here, although there are no control statements for the connection and command objects, the SqlDataAdapter object automatically constructs the corresponding SqlConnection and SqlCommand objects at the time of creation,

The connection is automatically initialized based on the connection string. Note that the SqlConnection and SqlCommand objects are turned off at this point.

(2) Create a DataSet object that needs to be populated with DataAdapter.

(3) Call the DataAdapter Fill method to populate the DataSet object with a DataTable.

Because the SQL statement in the command that follows the DataAdapter object is the user table in the Access database,

So when the Fill method is called, when the corresponding SqlConnection and SqlCommand objects are opened, a DataTable object named user is created with the data of the user table, and the DataTable is populated into the dataset.

The following code demonstrates how to use the DataAdapter object to update data in a dataset to a database.

Private Static stringstrconnect="data source=localhost;Uid=sa;pwd=aspent;database=logindb"stringSqlstr="SELECT * from USER";//using constructors, create DataAdapterSqlDataAdapter da=NewSqlDataAdapter (Sqlstr, strconnect);//Create a DataSetDataSet ds=NewDataSet ();//fill, the first parameter is the DataSet object to be populated, and the second parameter is the datatabble that fills the datasetDa. Fill (DS,"USER" );//The following code updates the data in the dataset//in a DataTable named "USER" in the dataset, add a DataRow object that describes the row recordDataRow Dr=ds. tables["USER"]. NewRow ();//adding a record through a DataRow objectdr["USERID"]="ID2";d r["USERNAME"]="TOM";d S. tables["USER"]. Rows.Add (DR);//update to the database.SqlCommandBuilder scb=NewSqlCommandBuilder (DA);d a.update (ds,"USER"); 

In the above code,

The DataSet object is first populated with DataAdapter.

Then, through the DataRow object, add a record to the dataset,

Finally, the added records are submitted to the database using the Update method of the dataset.

After executing the UPDATE statement, there is one more userid in the database user is ID2, username is Tom's record.

In addition, the SqlCommandBuilder object that appears in the code above is used to manipulate the data table.

With this object, you no longer have to use the DataAdapter Updatacommand property to perform the update operation.

C # and database Access Technology Summary (15) DataAdapter Object code example

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.