Intimate contact asp.net (9) storing multiple data tables in a dataset

Source: Internet
Author: User
Tags modify access database

Store multiple datasheets in a dataset we are accustomed to using the Recordset object to manipulate the database in our ASP, but the one disadvantage of the recordset is that a recordset can only store one datasheet, and when we need to manipulate multiple tables, Have to work back and forth in multiple recordsets, although these are not what they used to be, but for a novice, this is a very troublesome thing. Just those variable names can muddy you, now well, in the ASP.net, only need a dataset to get everything done. greatly facilitates our program. We are still the same as the old, first look at a program, and then to explain carefully.

<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.ADO"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Src,eventargs E)
{
file://Joint statement
String myConnString = "Driver={microsoft Access Driver (*.mdb)}; Dbq=c:/test/test.mdb; ";
String strComm1 = "SELECT * from UserList";
String strComm2 = "SELECT * from Booklist";

file:// Open a connection.
ADOConnection myconnection = new ADOConnection (myConnString);

file://two dozen DataSetCommand.
Adodatasetcommand MyComm1 = new Adodatasetcommand (strcomm1,myconnection);
Adodatasetcommand MyComm2 = new Adodatasetcommand (strcomm2,myconnection);

DataSet myDataSet = new DataSet ();

file://put Userlist,booklist table to DataSet
Mycomm1.filldataset (myDataSet, "userlist");
Mycomm2.filldataset (myDataSet, "Booklist");

DataGrid1.DataSource = mydataset.tables["UserList"]. DefaultView;
Datagrid2.datasource = mydataset.tables["Booklist"]. DefaultView;

Datagrid1.databind ();
Datagrid2.databind ();
}
</script>
<title></title>
<body>
<table>
<tr>
<td>
<asp:datagrid id= "DATAGRID1" runat= "Server"
Bordercolor= "BLACK"
borderwidth= "1"
gridlines= "Both"
cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
font-size= "8pt"
headerstyle-backcolor= "#aaaadd"
alternatingitemstyle-backcolor= "#eeeeee"
/>
</td>
<td>
<asp:datagrid id= "DataGrid2" runat= "Server"
bordercolor= "BLACK"
borderwidth= "1"
gridlines= "Both"
cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
headerstyle-backcolor= "#aaaadd"
alternatingitemstyle-backcolor= "#eeeeee"
/>
</td>
</tr>
</table>
</body>

In the example above, we open an Access database named Test.mdb, and then display two of his tables "userlist" and "Booklist" using two DataGrid controls. The pictures shown are as follows:

Figure 9-1

Let's analyze the code now:

String myConnString = "Driver={microsoft Access Driver (*.mdb)}; Dbq=c:/test/test.mdb; ";

String strComm1 = "SELECT * from UserList";

String strComm2 = "SELECT * from Booklist";

ADOConnection myconnection = new ADOConnection (myConnString);

Adodatasetcommand MyComm1 = new Adodatasetcommand (strcomm1,myconnection);

Adodatasetcommand MyComm2 = new Adodatasetcommand (strcomm2,myconnection);

These are just some preparation work, open a connection, and open two DataSetCommand to get two tables of data.

DataSet myDataSet = new DataSet ();

This is the key to our program, where we open the DataSet object we want to manipulate. Here we need to fill in the DataSet with the contents of the datasheet.

Mycomm1.filldataset (myDataSet, "userlist");

Mycomm2.filldataset (myDataSet, "Booklist");

Here is our main content today. The previous chapters have said that in a dataset can contain a variety of data, here we are to the dataset named myDataSet two database tables, in fact, as long as we can insert XML data inside, and they are not conflict. We can rest assured that the use, hehe.

The following code is to transfer the myDataSet data to the DataGrid control display. There's not much to say here.

Here we simply say that the dataset can hold multiple data tables, and we look at it as if nothing special. It does not seem to have any special features here, but if you match the changes, additions, and deletions in the dataset, it becomes easy to manipulate the database. We can first read the database into the dataset, and then modify the data in the dataset, if we do not feel satisfied with the change, we can use the RejectChanges method to fully recover, and finally to the database to update.

In the next section, we'll explain how to use a dataset to add, modify, and delete data.



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.