Intimate contact asp.net (10)

Source: Internet
Author: User
Tags one table first row
asp.net the section, our section focuses on how to use the dataset, add, modify, delete a number in the database
According

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 need
To understand the structure of the dataset, below 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 Datat
Able, 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, is actually add row row, here we also demonstrate
How to add a row of data, we program everything to the dataset as the apex, hehe, if tablescollection
, rowscollection down, there are some annoying dt. AcceptChanges the invocation of these methods is annoying
, it's time to take care of 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. And then we're in this number
According to the line, add the data we need. dr["UserName"] indicates that the UserName field is added and you can
Use dr[1] To add information, but this requires us to know in advance where the field is in the datasheet and without knowing the number
It is difficult to know the corresponding situation of the data we added, so it is better to use the field name as the table structure.

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, see 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 any errors in the dataset, and if so, use the RejectChanges () method to restore Dat
The data in the Aset. Note that recovery here is the data in all tables in the dataset and in the DataRow in the table, which is
All data in this hand-back operation are restored. If we only need to recover some of the content, we can use the DataTable
or DataRow rejectchanges (), here is not explained in detail, using the same method and DataSet, just a
It's a different kind of thing.

Probing the dataset for changes

We're 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)
{
Save
}else{
No action is done
}

Update Database

Our actions above are for the dataset only, not the database, but our goal is to
It is saved to the data, so we need to call the DataSetCommand Update method here. The following path
The sequence 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 more than one table, and we update only one
, then we must specify the updated datasheet name:

Mycomm.update (myDataSet, "userlist");

When the Update method is called, DataSetCommand compares the data in the database to the data in the dataset
To update the different places.

For the operation of the dataset, we only speak so much here, in fact, there are many methods and attributes of DataSet, and function
Very full, I think now here's the function, to 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.