Take it two projects, I feel very practical, take out everyone to study together, I think of the project, I will write it, there must be many places to improve, everyone will advise.
Using system;
Using system.data.sqlclient;
Using system.data;
Using system.configuration;
Namespace web.base_class
{
///
/// DB_CLASS summary description. CAUTION Be sure to call the destructor after using ExecuteReader
/// summary>
Public Class DB_CLASS
{
Private sqlconnection conn = new sqlConnection ();
///
/// private member
/// summary>
Private string _sql;
///
/// Property: Database query statement
/// summary>
Private string_er;
Public string ER
{
Get {return_er;}
}
Public String SQL
{
Get {return _sql;}
Set {_sql = value;
}
///
/// Constructor
/// summary>
Public DB_CLASS ()
{
//
// TODO: Add constructor logic here
//
Conn.connectionstring = system.configuration.configurationSettings.AppSettings ["constr"]. Tostring ();
}
~ db_class () // destructor
{
IF (Conn.State == ConnectionsState.Open)
CONN.CLOSE ();
}
///
// / Open the database connection
/// summary>
Public void db_open ()
{
IF (Conn.State == ConnectionsState.Closed)
{
Conn.connectionstring = system.configuration.configurationSettings.AppSettings ["constr"]. Tostring ();
Cn.open ();
}
}
///
/// Turn off the database connection
/// summary>
Public void db_close ()
{
IF (Conn.State == ConnectionsState.Open) {
CONN.CLOSE ();
Conn.dispose ();
}
}
///
/ / / Perform a non-return type query statement
/// summary>
///
Public Bool ExecuteSQL ()
{
SQLCommand cmd = new sqlcommand (_sql, conn);
THIS.DB_Open ();
Try
{
cmd.executenonquery ();
THIS.DB_Close ();
Return True;
}
Catch
{
THIS.DB_Close ();
Return False;
}
}
///
// / Execute a database query that returns DataReader
/// summary>
///
{
SQLCommand cmd = new sqlcommand (_sql, conn);
THIS.DB_Open ();
SqlDataReader DR;
Try
{
DR = cmd.executeReader (Commandbehavior.CloseConnection); // Parameter indicates that the DataGrid object will close the Connection object
IF (Dr.hasRows == True)
{
Return DR;
}
Else
{
_ER = "Data is empty";
Return NULL;
}
}
Catch
{
_ER = "Statement Error";
Return NULL;
}
}
///
///
/// summary>
///
Public DataView Executev ()
{
System.data.sqlclient.sqldataadapter Da = New SqldataAdapter (_SQL, CONN);
DataSet DS = New DataSet ();
THIS.DB_Open ();
Try
{
Da.fill (DS, "TB");
DataView DV = New DataView (ds.tables ["tb"]);
IF (dv.count> 0)
{
THIS.DB_Close ();
Return DV;
}
Else
{
THIS.DB_Close ();
_ER = "Data Error";
Return NULL;
}
}
Catch
{
_ER = "Data Error";
Return NULL;
}
Finally
{
THIS.DB_Close ();
}
}
Public String SQL2STR (String DEF)
{
String rr = "";
SQLCommand cmd = new sqlcommand (_sql, conn);
THIS.DB_Open ();
SqlDataReader DR;
Try
{
DR = cmd.executeReader (Commandbehavior.CloseConnection);
IF (Dr.hasRows)
{
Dr.read ();
RR = DR [0] .toString ();
Dr.close ();
THIS.DB_Close ();
}
}
Catch
{
RR = ""
}
IF (rr == "")
RR = DEF;
RETURN RR;
}
///
// / Execute the stored procedure for returning BOOL
/// summary>
///
Public Bool EXEPRO ()
{
THIS.DB_Open ();
SQLCommand cmd = new sqlcommand (_sql, conn);
cmd.commandtype = commandtype.storedProcedure;
Try
{
cmd.executenonquery ();
THIS.DB_Close ();
Return True;
}
Catch
{
THIS.DB_Close ();
Return False;
}
}
}
}