Intimate contact asp.net (10) Add Modify delete data

Source: Internet
Author: User
Tags modify first row

In the next section, our section focuses on how to use datasets to add, modify, and delete data in a database.

First we need to open a connection, our database is still in the previous section of the bar:

String myConnString = "Driver={microsoft Access Driver (*.mdb)}; Dbq=c:/test/test.mdb; ";
String Strcomm = "SELECT * from UserList";
ADOConnection myconnection = new ADOConnection (myConnString);
Adodatasetcommand Mycomm = new Adodatasetcommand (strcomm,myconnection);

Here we are in order to explain conveniently, only in the dataset into a table content:

DataSet myDataSet = new DataSet ();
Mycomm.filldataset (myDataSet, "userlist");

At this point we get a DataSet with UserList table data. Before we explain the dataset, we also need to understand the structure of the dataset, which is the structure tree of the dataset.

DataSet
Relationscollection
ExtendedProperties
Tablescollection
DataTables
 
Rows
Columns
Other
Because we're studying the DataTable, the rest of us don't care about them for a while. A dataset contains multiple DataTable, and a DataTable contains multiple row, which is the basis of our operation DataSet:

Add data

Add a data, from the above list we can see, in fact, is to add a row, here we also demonstrate how to add a row of data, we program everything to a dataset as the apex, hehe, if tablescollection,rowscollection down, and some annoying dt. AcceptChanges These methods of call, very annoying, or a fix it.

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 row of data that stores our newly added data. Then we add the data we need in this line of data. dr["UserName"] indicates that the UserName field is added, you can use dr[1 to add the information, but this requires us to know in advance the location of the field in the datasheet, and it is difficult to know the corresponding situation of the data we added without knowing the structure of the datasheet. So it's better to use the field name.

Finally, we use the Rowscollection Add method to add our new line to the datasheet.

modifying data

When you know how to add data, it's easy to modify the data.

mydataset.tables["UserList"]. rows[0]["UserName"]= "flying brother";

This allows us to modify the username field in the first row of data.

Delete data

Delete the data, mainly by using the rowscollection provided by the Delete method, look at the following program is also very simple things:

mydataset.tables["UserList"],rows[0]. Delete ();

This line of data has been deleted.

Recover data

Sometimes we get an error when we add/modify data, and then we need to restore the original data. The following procedure shows how to determine if an error occurred:

if (mydataset.haserrors)
{
Mydataset.rejectchanges ();
}
 
First we check if there are errors in the dataset, and if so, use the RejectChanges () method to recover the data in the dataset. Note that the recovery here is the data in all tables in the dataset and in the DataRow in the table, that is, the data in this secondary operation is restored. If we only need to recover some of the content, we can use the DataTable or DataRow RejectChanges (), which is not explained in detail here, using the same method as the dataset, only the operation is different.

Probing the dataset for changes

We are sending the dataset to the database to save it, and we need to see if the dataset has been altered. If there is no change, we will not need to modify the database.

if (mydataset.haschanges)
{
file://Security Deposit
}else{
file://not doing anything
}

Update Database

Our actions above are for the dataset only, not manipulating the database, but our goal is to save the data to the data, so we need to call the DataSetCommand Update method here. The following program shows how to give the dataset data to the database.

Mycomm.update (myDataSet);

A very simple sentence, hehe. Note here that if a dataset contains multiple tables, and we only update one, then we must specify the updated datasheet name:

Mycomm.update (myDataSet, "userlist");

When the Update method is invoked, DataSetCommand compares the data in the database to the data in the dataset and updates the different places.

For the operation of the dataset, we only talk about so much, in fact, there are many methods and properties of DataSet, the function is also very full, I think now here the function, the general operation is enough.



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.