Contact ASP.Net(10 intimately)
Tuesday, March 03, 2009 by rain
Above all we need to open a tie,Our database still uses on one:)
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);
We go to the lavatory to explain here,Stock the content of a watch in DataSet only:
DataSet MyDataSet = New DataSet();
MyComm.FillDataSet(MyDataSet, "UserList" );
The DataSet that right now we obtained to have UserList to express data.Before explaining DataSet,The structure that DataSet is below the structure that we still need to understand DataSet is cultivated
DataSet
RelationsCollection
ExtendedProperties
TablesCollection
DataTables
Rows
Columns
Other
What study as a result of us is DataTable,Of other we are temporary without giving thought to them.Many DataTable are included in a DataSet, a DataTable is included again have many Row,This is the base that we operate DataSet:)
Add data
Add one data,From inside the list above we can see,Add a Row namely actually,We also demonstrate how to add a data here,Our program everything is acme with DataSet,Breathe out ah,If TablesCollection, if RowsCollection goes down,Still have a few bored Dt.AcceptChanges of these methods call,Very bored,Still do calm.
DataRow Dr=MyDataSet.Tables["UserList" ].NewRow();
" ; of dispatch of week of Dr["UserName" ] = "
Dr["ReMark" ] = "100" ;
Beautiful MM" of Dr["Comment" ] = " ;
MyDataSet.Tables.Rows.Add(dr);
group,We build a new data to go,It is used store we are new affiliation data.Next the data that we join us to need in this data travel.Dr["UserName" ] makes clear is to UserName the field undertakes additive,You can use Dr[1] to add information,But the place that this needs us to know the field is expressed in data beforehand,And the corresponding circumstance of the data that knows in the condition that does not know data expresses a structure we are added very hard,Still use field name so had better.
The Add method that we use RowsCollection finally,In building us this group to join data to express.
Modification data
After knowing how to add data,Modification data also is very simple thing.
" ; of eldest brother of flying knife of MyDataSet.Tables["UserList" ].Rows[0]["UserName" ]= "
Such we revised the UserName field in the first data.
Delete data
Cutout divisor is occupied,Basically be the Delete means that use RowsCollection provides,Seeing the program below also is very simple thing:)
MyDataSet.Tables["UserList" ] , rows[0].Delete();
This one data had been deleted.
Restore data
Occasionally we are added / modification data can appear mistake,At that time,Restore original data with respect to need.The program below,Show how to judge whether to have mistake happening:
If(MyDataSet.HasErrors)
{
MyDataSet.RejectChanges();
}
Whether is there mistake happening in our examination DataSet above all,If have with respect to use RejectChanges() method,The data in restoring DataSet.The data in the DataRow in noticing here restores is all in DataSet watch and watch,The data that makes second operation here namely restores entirely.If we need to restore partial content only,The RejectChanges() that we can use DataTable or DataRow,Did not explain in detail here,Use method and DataSet are same,What just operate is different to resembling just.
Whether does exploration DataSet have alter
We are going to DataSet deliver saving to the database go,We need to saw this DataSet whether had been altered.If did not alter,We also were not necessary to revise a database.
If(MyDataSet.HasChanges)
{
/ / save
}else{
/ / do not have any operations
}
Update a database
The operation above us,Just be aimed at DataSet,According to the library without operand,But our purpose or in wanting to save data data, go,The Update method that DataSetCommand calls with respect to need here so.The program below shows how to give the data of DataSet database.
MyComm.Update(MyDataSet);
A very simple,Breathe out ah.Should notice here,If the bag in a DataSet contains many lists,And we update only,Then we must mention expressly updated data expresses a name:
MyComm.Update(MyDataSet, "UserList" );
After Update method is called,The data phase in the data in database of DataSetCommand meeting general and DataSet is compared,Undertake updating to not same place.
To the operation of DataSet,So much is told only here,Actually the method of DataSet and attribute have a lot of,The function is very complete also,The function that I think to the place here is told,Already enough to general operation.