Next, we will discuss how to use DataSet to add, modify, and delete a number in the database.
Data.
First, we need to open a connection. Our database should use the previous section :)
String MyConnString = "Driver = {Microsoft Access Driver (*. mdb )};
DBQ = c:/test. mdb ;";
String strComm = "select * from UserList ";
ADOConnection MyConnection = new ADOConnection (MyConnString );
ADODataSetCommand MyComm = new ADODataSetCommand (strComm, MyConnection );
For the sake of convenience, we only store the content of one table in DataSet:
DataSet MyDataSet = new DataSet ();
MyComm. FillDataSet (MyDataSet, "UserList ");
In this case, a DataSet with UserList table data is obtained. Before explaining DataSet, we still need
To understand the DataSet structure, the following is the DataSet structure tree.
DataSet
RelationsCollection
ExtendedProperties
TablesCollection
DataTables
Rows
Columns
Others
Since we are studying DataTable, we don't care about them for the moment. A DataSet contains multiple DataT
A DataTable contains multiple rows, which is the basis for DataSet operations :)
Add data
We can see from the list that adding a Row is actually adding a Row. Here we will also demonstrate
How to add a row of data, we use DataSet as the vertex, huh, if TablesCollection
If RowsCollection goes down, there are some annoying methods called such as dt. AcceptChanges, which are annoying.
You can do it all at once.
DataRow dr = MyDataSet. Tables ["UserList"]. NewRow ();
Dr ["UserName"] = "Zhou Xun ";
Dr ["ReMark"] = "100 ";
Dr ["Comment"] = "Beautiful mm ";
MyDataSet. Tables. Rows. Add (dr );
In the first line, we create a new data row to store the newly added data. Then, in this number
Add the required data to the data row. Dr ["UserName"] indicates that the UserName field is added. You can