Actions in the database in ASP.NET ---- DataSet Operating Database

xiaoxiao2021-03-31  213

As we have already said how to operate the database, but almost all through OLEDBCommand and OLEDBDataReader, this time we talk about how to operate the database through OledbDataAdapter! About OLEDBDATAADAPTER usually we have already said before, because OLEDATAADAPTER is a bridge to establish contact between DataSet and data sources, and DataSet is used to store, remote processing, and programming of single-layer data, XML data, and relational data. ! . We have been using Command to increase, delete, and modify the database, but we use Dataset and DataAdapter to make more convenient to operate the database, basically we can think that DataSet is designed specifically for web, this is also ADO. An important difference from NET and ADOs. Below is the relationship diagram of DataSet and SQL data, and we can see the relationship between DataSet and DataAdapter and SQL databases through this chart.

Let's explain how to use Dataset and DataAdapter to operate the database myconnection.open (); // Open the database, please refer to the contents of the previous article myCommand.connection = myConnection; // Set your command, please refer to the contents of the previous article MyCommand . CommandText = "select * from admin"; // set Command, reference to the previous contents of the article OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter (); // defines OleDbDataAdapte objects MyDataAdapter.SelectCommand = MyCommand; // set the SelectCommand property object System.Data OleDbDataAdapte .Dataset mydataset = new system.data.dataset (); // Defines dataset mydataadapter.fill (MyDataSet, "Admin"); // Plip the MyDataSetmyConnection.Close (); // Turns the database with the selectcommand attribute of the OLEDBDataAdapte object.

The whole process is divided into the following steps: 1. Establish a database connection (open the database, refer to the contents of the previous article) 2. Establish an OLEDBDataAdapter object! 3. Institiate the OLEDBDataAdapter object! 4. Create a DataSet object, perform the table obtained by the SQL statement is added to 5. Turning off Database Connections With the steps above We can use Database to bind data in the DataSet to a specific control! (The next chapter we will explain how the Bond Database) We have said that we can use Dataset and DataAdapter to make more convenient to operate the database, how to perform the database's operations through OLEDBDataAdapter, we only need to perform data in DataSet Add, delete, modify, etc., then submit the DataSet to the database, you can use the Dataset and DataAdapter operation database public boolean dodb () {myConnection.Open (); // Open the database, please refer to the contents of the previous article MyCommand .Connection = MyConnection; // set Command, please refer to the previous article content MyCommand CommandText = "select * from admin";. // set Command, content OleDbDataAdapter refer to the previous article MyDataAdapter = new OleDbDataAdapter (); // define objects OleDbDataAdapte MyDataAdapter.SelectCommand = MyCommand; // set OleDbDataAdapte object SelectCommand property System.Data.DataSet MyDataSet = new System.Data.DataSet (); // define DataSet MyDataAdapter.Fill (MyDataSet, "admin"); // objects by OleDbDataAdapte SelectCommand property filled MyDataSetOleDbCommandBuilder MyCommandBuild = new OleDbCommandBuilder (MyDataAdapter); // DataSet and operatively associated database, necessary foreach (DataRow dr in MyDataSet.Tables [ "Admin"] Rows.) {if (dr [ "Admin_Code" ] .ToString (). Trim (). Equals ("a")) {Dr.Delete (); // Delete rows in DataSet}} myDataSet.Tables ["admin"]. Rows [0] [0] = "SS"; // update the value string in the first line of the first line in DataSet [] DD = New string [3] {"a", "b", "v"}; mydataset.tables ["admin"]. rows.add (dd); // Increases a line MyDataAdapter.Update (MyDataSet, "Admin"); // Submit the data in the "admin" table in the DataSet to the database, complete the database update.close (); // Turn off the database}

This program and us used to use Command's delete, INSERT, and UPDATE routines are performing the same function, I changed here to the same effect with MyDataAdapter!

To perform the operation of the database via MyDataAdapter, we have to have the following steps: 1. Establish a database connection with MyConnection2. Institiate the OLEDBDataAdapter object! 3. Create a DataSet object and add a record obtained by the SELECT statement to it 4. Establish an OLEDBCommandbuilder object! And let it associate it with the OLEDBDataAdapter object in front! The statement is as follows: oledbcommandbuilder mycommandbuild = new oledbcommandbuilder (myDataAdapter); 5. Add, delete, modify specific records containing tables in DataSet

6. Execute the Update command for the OLEDBDataAdapter object to update the database, the statement is as follows: MyDataAdapter.Update (DS, "Notes"); 7. Turn off database connection

Summary: DataSet is a very important content in ADO.NET, and an important manifestation of the difference between ADO.NET and ADO, especially suitable for batch data operations, but also an important source of data. OLEDBDataAdapter is a bridge that establishes contact between DataSet and data sources. To be skilled using DataSet we need to master OledbDataAdapter. Next chapter we will tell DATA

转载请注明原文地址:https://www.9cbs.com/read-130763.html

New Post(0)