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