I learned from Niu Ren about using and database operations and pay attention to releasing resources.

Source: Internet
Author: User

I learned from Niu Ren about using and database operations and pay attention to the release of resources. These are usually used directly. I didn't think why is it better? The following sections Code We will start with the Code. (Of course, it is about the above theme. I think it is my review. Here I deeply feel that some people have pointed out and discussed it better than finding information from the Internet, it is much more efficient for a person to learn. Thank you for your advice.) protected void page_load (Object sender, eventargs e) {string CCON = new sqldataprovider (). connectionstring; sqlconnection con = new sqlconnection (CCON); sqldataadapter da = NULL; try {If (! Ispostback) {con. open (); string osql = "select bookid, telnumber, bwk_120bookin.createddate, 'createdbyusername' = users. firstname + users. lastname, bwk_120calltype.calltypename, bwk_120symptom.symptomname, sickingplace, carplace from bwk_120bookin inner join users on bwk_120bookin.createdbyuser = users. userid inner join bwk_120calltype on bwk_120bookin.calltypeid = bwk_120calltype.calltypeid inner Join bwk_120symptom on bwk_120bookin.symptomid = Middle "; // extract data from the database da = new sqldataadapter (osql, con); // create a DataSet object dataset DS = new dataset (); // load the extracted data from the database to the record set da. fill (DS, "bookin"); // retrieves the table object datatable dt = ds from the record set. tables ["bookin"]; // you can specify the string filterexpression = "createdbyusername = 'superuseraccount '"; // set the fields to be sorted and the sorting method string sortexpression = "bookid ASC"; // line-like The State filter is set to the original row dataviewrowstate rowstatefilter = dataviewrowstate. originalrows; // dataview DV = new dataview (DT, filterexpression, sortexpression, rowstatefilter ); // create a dataview object and PASS Parameters to its constructor // set the attributes of table, rowfilter, sort, and rowstatefilter respectively after dataview is generated. // The following is the step-by-step setting of the dataview object DV = new dataview (); // obtain the data DV. table = DT; // sets the filtering attribute DV. rowfilter = filterexpression; // sets the sorting attribute DV. sort = sortexpression; // execute filtering DV. rowstate Filter = rowstatefilter; gridview1.datasource = DV; gridview1.databind (); // reportviewer1.localreport. reportpath = strpart} catch (exception ex) {exceptions. processmoduleloadexception (this, ex);} finally {con. close (); DA. dispose () ;}} the above Code uses finally to close the database connection and sqldataadapter. Finally is executed regardless of whether the code is executed incorrectly. This ensures that the database resources are released and will not cause database problems. I used to think that all the things had. NET Framework to clean up the mess and write it casually. Today I Know That. NET Framework cannot manage some database things. When you have to write your own code, consider it. This cool man is still thinking about other solutions. There are many solutions, and I am very impressed with the use of using. I have seen using in the code before, but I have not really understood the role of using in implementing the Code. Okay, let's take a look at the using implementation version of the above Code: protected void page_load (Object sender, eventargs e) {try {If (! Ispostback) {string CCON = new sqldataprovider (). connectionstring; using (sqlconnection con = new sqlconnection (CCON) {con. open (); string osql = "select bookid, telnumber, bwk_120bookin.createddate, 'createdbyusername' = users. firstname + users. lastname, bwk_120calltype.calltypename, bwk_120symptom.symptomname, sickingplace, carplace from bwk_120bookin inner join users on bwk_120bookin.createdbyu SER = users. userid inner join bwk_120calltype on counter = bwk_120calltype.calltypeid inner join bwk_120symptom on counter = forward "; // extract data from the database using (sqldataadapter da = new sqldataadapter (osql, con )) {// create and declare the record set object dataset DS = new dataset (); // load the extracted data from the database to the record set da. fill (DS, "bookin"); // closes the database // retrieves the table object datatable dt = ds from the record set. tables ["bookin"]; // Set the string filterexpression = "createdbyusername = 'superuseraccount'"; // set the fields to be sorted and the string sortexpression = "bookid ASC "; // set row state filtering to the original row dataviewrowstate rowstatefilter = dataviewrowstate. originalrows; // dataview DV = new dataview (DT, filterexpression, sortexpression, rowstatefilter ); // create a dataview object and PASS Parameters to its constructor // After dataview is generated, set the attributes of table, rowfilter, sort, and rowstatefilter. // The following is step-by-step settings. // create a dataview pair Like DV = new dataview (); // obtain the data DV. table = DT; // sets the filtering attribute DV. rowfilter = filterexpression; // sets the sorting attribute DV. sort = sortexpression; // execute filtering DV. rowstatefilter = rowstatefilter; gridview1.datasource = DV;} gridview1.databind () ;}// reportviewer1.localreport. reportpath = strpart} catch (exception ex) {exceptions. processmoduleloadexception (this, ex) ;}} in this way, the dispose of this object is automatically called as long as the code segment is left. When a CCON object error occurs, it jumps out of the {} range of the using and dispose. Similarly, when a DA error occurs, it also occurs. Even if it is executed normally, it will dispose the corresponding object when it is executed outside the scope of using.

First: http://metababy.blogspot.com/2009/04/using.html

 

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.