From the cows. The issues of using and database operations to pay attention to releasing resources these are usually used directly, not to think why this, there is a better implementation.
Here are some of the code, we will be the code to start (of course, on the above topic, as my review bar, in this deeply felt that someone to guide and discussion is more than a person from the Internet to find information, a person to learn to be more efficient. Thanks for the advice of the two bulls.
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,b Wk_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 = bwk_ 120symptom.symptomid "; //data extracted from database &N Bsp da = new SqlDataAdapter (OSql, con); //Create and declare Recordset objects   dataset ds = new DataSet (); //The data extracted from the database into the recordset & nbsp da. Fill (ds, "Bookin");  &NBS P //from recordset Get Table object datatable dt = ds. tables["Bookin"]; //Set the condition string to be filtered string filterexpression = "createdbyusername= ' Superuseraccount '"; //set the fields to sort and sort by strings string SortExpression = "BookID ASC"; //line status filter set to previous line dataviewrowstate & nbsp rowstatefilter = dataviewrowstate.originalrows;
//dataview dv=new DataView (dt,filterexpression , Sortexpression,rowstatefilter); // Create a DataView object and pass parameters to its constructor // Set table, RowFilter, Sort, RowStateFilter properties after generating DataView //Below is a step-by-step setting //Create Datavie W objects  DV = new DataView (); //get data  DV. Table = DT; //setting filter properties &NBSP ; &NBSp  DV. RowFilter = FilterExpression; //setting sort properties &NBSP ;  DV. Sort = SortExpression; //execution filter  DV. RowStateFilter = RowStateFilter;
Gridview1.datasource = DV; Gridview1.databind (); ReportViewer1.LocalReport.ReportPath =strpart} catch (Exception ex) {exceptions.processmoduleloadexception (this, ex); finally {con. Close (); Da. Dispose ();
} }
The code above is to use finally to close the database connection and SqlDataAdapter. Finally, no matter whether the code executes correctly or not, it will be executed, which guarantees the release of the database resources and does not cause the database problem. I thought, all things, have the. NET Framework to clean up the pieces, casually write, today to know that some things about the database, the. NET Framework is not in control. You have to write your own code, considering.
The bull is still considering other solutions, and I'm impressed by the use using. I've seen the use using in the code before, but I don't really understand the role of using in implementing code.
Well, let's 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 (c Con) { &NBSP ; con. Open (); string oSql = "Select BookID, &NB Sp 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 = bwk_ 120symptom.symptomid "; //extract data from database using (SQL DataAdapter da = new SqlDataAdapter (OSql, con)) &NBSP ; { //Create and declaration Recordset object data Set ds = new DataSet (); //loading data extracted from the database into a recordset da. Fill (ds, "Bookin"); //Close database
//get Table object from recordset datatable dt = ds. tables["Bookin"]; //Set the condition string to filter   ; string Filterexpres sion = "Createdbyusername= ' Superuseraccount '"; //set the field and sort string to sort by nbsp string Sortexpressi on = "BookID ASC"; //line status filter set to previous line   ; &NBSP dataviewrowstate &N Bsp rowstatefilter = dataviewrowstate.originalrows;
//dataview dv=new DataView (dt,filterexpression,sortexpression,rowstatefilter); //create DataView object and pass parameters to its constructor & nbsp //set table, RowFilter, Sort, RowStateFilter properties after generating DataView nbsp //Below is a step-by-step setup of &NB sp; //Create DataView object & nbsp;  DV = new DataView (); //get data   ;    DV. Table = DT; //setting filter Properties &NBS p;  DV. RowFilter = FilterExpression; //setting sort properties &nbs p;  DV. Sort = SortExpression; //execute filter  DV. RowStateFilter = RowStateFilter;
Gridview1.datasource = DV; } gridview1.databind (); }//reportviewer1.localreport.reportpath =strpart}
catch (Exception ex) {exceptions.processmoduleloadexception (this, ex); }
}
This enables you to automatically call the dispose of this object as soon as you leave the code snippet.
When the Ccon object goes wrong, it immediately jumps out of the using {} range and Dispose, likewise, when the DA is wrong. Even if it is performed normally, the corresponding object is then dispose when it is outside the scope of the using {}. Original start: http://metababy.blogspot.com/2009/04/using.html