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