Contact ASP.Net(9 intimately)
Tuesday, March 03, 2009 by rain
Store in a DataSet many data table
We are used to using RecordSet object to operate a database in a lot of people in ASP,But RecordSet has drawback is a RecordSet can store only a data is expressed,When we need to operate many watches,Must operate back and forth in many RecordSet,Whats don't although these is in after use habit, have,But to a novice,The thing that this also is a very troublesome person.Light is those variable names can do whole you,good now,In ASP.Net,Need a DataSet to be able to be done only decide everything.Greatly went to the lavatory our program.We or old pattern,See a paragraph of program first,Come again fine fine explain.
%26lt;% @ Page Language= %26quot;C#%26quot; %%26gt;
%26lt;% @ Import Namespace= %26quot;System.Data%26quot; %%26gt;
%26lt;% @ Import Namespace= %26quot;System.Data.ADO%26quot; %%26gt;
%26lt;Script Language= %26quot;C#%26quot; Runat= %26quot;Server%26quot; %26gt;
Public Void Page_Load(Object Src, eventArgs E)
{
/ / coupling statement
String MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/test/test.mdb;" ;
String StrComm1 = "select * From UserList" ;
String StrComm2 = "select * From BookList" ;
/ / open a tie
ADOConnection MyConnection = New ADOConnection(MyConnString);
/ / open two DataSetCommand
ADODataSetCommand MyComm1 = New ADODataSetCommand(strComm1, myConnection);
ADODataSetCommand MyComm2 = New ADODataSetCommand(strComm2, myConnection);
DataSet MyDataSet = New DataSet();
/ / UserList, bookList watch stocks 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();
}
%26lt;/script%26gt;
%26lt;html%26gt;
%26lt;head%26gt;
%26lt;title%26gt;%26lt;/title%26gt;
%26lt;/head%26gt;
%26lt;body%26gt;
%26lt;table%26gt;
%26lt;tr%26gt;
%26lt;td%26gt;
%26lt;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 "
/ %26gt;
%26lt;/td%26gt;
%26lt;td%26gt;
%26lt;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 "
/ %26gt;
%26lt;/td%26gt;
%26lt;/tr%26gt;
%26lt;/table%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
In above example,We opened an Access database that the name is Test.mdb,Use his among them two watches "UserList" and "BookList" next two DataGrid accuse show come.Indication picture is as follows:
Graph 9-1
We analyse 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 just are making a few preparation work,Open a tie,The data that and open two DataSetCommand obtains two lists.
DataSet MyDataSet = New DataSet();
The crucial ground that this is our program,The DataSet object that we opened to want to operate here.The content that we express data with respect to need below filled DataSet.
MyComm1.FillDataSet(MyDataSet, "UserList" );
MyComm2.FillDataSet(MyDataSet, "BookList" );
We are here today's main content.A few chapters in front once had said,A variety of data can be included in a DataSet,We are to go to here two databases were deposited to express in this DataSet that the name is MyDataSet,Should be willing only actually,We still can insert XML data inside,And they are to won't appear of conflict.Everybody can be at ease use,Breathe out ah.
Again the code below,Convey the data of MyDataSet to DataGrid to accuse namely show.Did not say more here.
We are the functions that said DataSet can deposit many data express simply only here,Everybody looks value picture without what special.The function that seems to be fastened without what spy really it seems that here,But if deserve to go up the modification in DataSet, add, delete a function,We operate a database to become very simple.We can read in the database DataSet first,Revise data in DataSet next,If change so that feel dissatisfactory,We still can use RejectChanges method to restore in the round,Give along with all the others finally go the database goes updating.
Below one,We explain again,How to use DataSet to add,Revise,Cutout divisor is occupied.