Tip Note: DataSet update function

Source: Internet
Author: User
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.

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.