From: http://www.cnblogs.com/enuosky/archive/2006/05/15/400879.aspx
Modify the data volume of a classic SQL server that was previously collected.
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb;
/// <Summary>
/// Summary of dataaccess
/// </Summary>
Public class dataaccess
{
Protected static oledbconnection conn = new oledbconnection ();
Protected static oledbcommand comm = new oledbcommand ();
Public dataaccess ()
{
// Init
}
Private Static void openconnection ()
{
If (conn. State = connectionstate. Closed)
{
Conn. connectionstring = @ "provider = Microsoft. Jet. oledb.4.0; Data Source =" + configurationmanager. etettings ["myconn"]; // set in the web. config file.
Comm. Connection = conn;
Try
{
Conn. open ();
}
Catch (exception E)
{Throw new exception (E. Message );}
}
} // Open the database
Private Static void closeconnection ()
{
If (conn. State = connectionstate. open)
{
Conn. Close ();
Conn. Dispose ();
Comm. Dispose ();
}
} // Close the database
Public static void excutesql (string sqlstr)
{
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Comm. executenonquery ();
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{Closeconnection ();}
} // Execute the SQL statement
Public static oledbdatareader datareader (string sqlstr)
{
Oledbdatareader DR = NULL;
Try
{
Openconnection ();
Comm. commandtext = sqlstr;
Comm. commandtype = commandtype. text;
Dr = comm. executereader (commandbehavior. closeconnection );
}
Catch
{
Try
{
Dr. Close ();
Closeconnection ();
}
Catch {}
}
Return Dr;
} // Return the oledbdatareader object of the specified SQL statement. Close this object when using it.
Public static void datareader (string sqlstr, ref oledbdatareader Dr)
{
Try
{
Openconnection ();
Comm. commandtext = sqlstr;
Comm. commandtype = commandtype. text;
Dr = comm. executereader (commandbehavior. closeconnection );
}
Catch
{
Try
{
If (Dr! = NULL &&! Dr. isclosed)
Dr. Close ();
}
Catch
{
}
Finally
{
Closeconnection ();
}
}
} // Return the oledbdatareader object of the specified SQL statement. Disable it when using it.
Public static dataset (string sqlstr)
{
Dataset DS = new dataset ();
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DS );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
Return Ds;
} // Return the dataset of the specified SQL statement
Public static void dataset (string sqlstr, ref dataset DS)
{
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DS );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
} // Return the dataset of the specified SQL statement
Public static datatable (string sqlstr)
{
Datatable dt = new datatable ();
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DT );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
Return DT;
} // Return the datatable of the specified SQL statement
Public static void datatable (string sqlstr, ref datatable DT)
{
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DT );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
} // Return the datatable of the specified SQL statement
Public static dataview (string sqlstr)
{
Oledbdataadapter da = new oledbdataadapter ();
Dataview DV = new dataview ();
Dataset DS = new dataset ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DS );
DV = Ds. Tables [0]. defaultview;
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
Return DV;
}
// Return the dataview of the specified SQL statement
}