Preliminary database operations in ASP. NET (2)

Source: Internet
Author: User

Database Operations in ASP. NET ---- DataSet database operations


We have already discussed how to operate databases, but almost all of them are done through OleDbCommand and OleDbDataReader. This time we will talk about how to operate databases through OleDbDataAdapter! We have already discussed the use of OleDbDataAdapter before. Because OleDbDataAdapter is a bridge between DataSet and the data source, dataSet is used to store, remotely process, and program single-layer data, XML data, and relational data !.
We have talked about the use of Command to add, delete, and modify databases. However, DataSet and DataAdapter can be used to operate databases more conveniently, basically, we can think that DataSet is designed specifically for the WEB, which is also ADO. NET and ADO.
The following figure shows the relationship between DataSet and SQL data. We can see the relationship between DataSet, DataAdapter, and SQL database.


The following describes how to use DataSet and DataAdapter to operate databases.
MyConnection. Open (); // Open the database. Refer to the content in the previous article.
MyCommand. Connection = MyConnection; // set Command. Refer to the content in the previous article.
MyCommand. CommandText = "select * from admin"; // set Command. Refer to the content in the previous article.
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter (); // defines the OleDbDataAdapte object
MyDataAdapter. SelectCommand = MyCommand; // you can specify the SelectCommand attribute of the OleDbDataAdapte object.
System. Data. DataSet MyDataSet = new System. Data. DataSet (); // defines DataSet
MyDataAdapter. Fill (MyDataSet, "admin"); // Fill in MyDataSet through the SelectCommand attribute of OleDbDataAdapte object
MyConnection. Close (); // Close the database

The entire process is divided into the following steps:
1. Establish a database connection (open the database, please refer to the content in the previous article)
2. Create OleDbDataAdapter object!
3. instantiate OleDbDataAdapter object!
4. Create a DataSet object and add the table obtained by executing the SQL statement to it.
5. Close database connection
Through the above steps, we can use DataBind to bind data in DataSet to a specific control! (The next chapter describes how to attach databases)
We have said, but we can use DataSet and DataAdapter to perform database operations more conveniently. How can we use OleDbDataAdapter to perform database operations, you only need to add, delete, and modify the data in DataSet, and then submit the data to the database.
// Use DataSet and DataAdapter to operate the database
Public Boolean DoDB ()
{
MyConnection. Open (); // Open the database. Refer to the content in the previous article.
MyCommand. Connection = MyConnection; // set Command. Refer to the content in the previous article.
MyCommand. CommandText = "select * from admin"; // set Command. Refer to the content in the previous article.
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter (); // defines the OleDbDataAdapte object
MyDataAdapter. SelectCommand = MyCommand; // you can specify the SelectCommand attribute of the OleDbDataAdapte object.
System. Data. DataSet MyDataSet = new System. Data. DataSet (); // defines DataSet
MyDataAdapter. Fill (MyDataSet, "admin"); // Fill in MyDataSet through the SelectCommand attribute of OleDbDataAdapte object

OleDbCommandBuilder MyCommandBuild = new OleDbCommandBuilder (MyDataAdapter); // operations related to DataSet and databases are essential
Foreach (DataRow dr in MyDataSet. Tables ["Admin"]. Rows)
{
If (dr ["Admin_Code"]. ToString (). Trim (). Equals (""))
{
Dr. Delete (); // Delete a row in DataSet
}
}
MyDataSet. Tables ["Admin"]. Rows [0] [0] = "ss"; // update the value of the first column of the First row in DataSet
String [] dd = new String [3] {"a", "B", "v "};
MyDataSet. Tables ["Admin"]. Rows. Add (dd); // Add a row
MyDataAdapter. Update (MyDataSet, "Admin"); // submit the data in the "Admin" table in DataSet to the database to complete database updates.
MyConnection. Close (); // Close the database
}

This program is used in the same way as the delete, insert, and update routines of Command. I changed it to MyDataAdapter to achieve the same effect!

To use MyDataAdapter to perform database operations, take the following steps:

1. Create a database connection MyConnection
2. instantiate OleDbDataAdapter object!
3. Create a DataSet object and add the records obtained by executing the select statement to it.
4. Create OleDbCommandBuilder object! And associate it with our previous OleDbDataAdapter object! The statement is as follows: OleDbCommandBuilder MyCommandBuild = new OleDbCommandBuilder (MyDataAdapter );
5. add, delete, and modify specific records in the DataSet table.

6. Execute the Update command of the OleDbDataAdapter object to Update the database. The statement is as follows: MyDataAdapter. Update (ds, "notes ");
7. Close database connection


Related Article

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.