If some content of the dataset in the data set changes, how does one reflect the database and update the changed content at the same time? We all know that the content in the data set dataset is bound to the DataGrid control. If you perform data insertion, deletion, update, and other operations, do you need to submit the changed results to the database for each change? Obviously, it is not a good solution, and the efficiency is not high. What should I do?
You can read data from a database to a dataset at a time, and use a data set everywhere, that is, read data at a time and use it everywhere :)))
The data set must be known:Private Static dataset DT;
// Data operation class
Access oledb = new access ();
When the page is loaded for the first time:
If (! This. ispostback)
{
Dt = oledb. binddata ("select * from digest order by did DESC", "Digest ");
Bindthegrid ();
}
Function: bindthegrid () is used to bind a data list control.Code:
// Bind the table
Private void bindthegrid ()
{
Rssdatagrid. datasource = DT. Tables [0]. defaultview;
Rssdatagrid. databind ();
}
In this way, only operations are performed on the current dataset during addition, update, or deletion, and a button is placed. the text attribute is "Save all changes". In this way, the data set is submitted and the database is updated at one time:
Private Void Saveallsetingbtn_click ( Object Sender, system. eventargs E)
{
Try
{< br> oledb. updatedataset (DT, " digest " , " select * from digest " );
response. write ( " " );
}
Catch
{
Response. Write ("<SCRIPT> alert ('Update failed! Check your input! ') </SCRIPT>");
}
}
The corresponding data set update function of its data operation class is written as follows: /// <Summary>
/// Update the table in the bound Database
/// </Summary>
/// <Param name = "ds"> Dataset parameters to be updated </Param>
/// <Param name = "tablename"> The name of the Response Table in dataset, corresponding to the table in the database </Param>
/// <Returns> </returns>
Public Dataset updatedataset (Dataset ds, String Tablename, String Handlestring)
{
Oledbcommand oC = Returnoledbcommand (handlestring );
Oledbdataadapter Adapter = New Oledbdataadapter ();
Adapter. selectcommand = OC;
Oledbcommandbuilder = New Oledbcommandbuilder (adapter );
Adapter. Update (DS, tablename );
OC. Connection. Close ();
DS. acceptchanges ();
Return DS;
}
In this way, the data set is updated, and the database is only accessed twice before and after.
If you use application to save data sets, it seems that it is not suitable for sites with low access volumes.