I accumulated database operation class

xiaoxiao2021-03-06  43

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

///

Public Class DB_CLASS

{

Private sqlconnection conn = new sqlConnection ();

///

/// private member

///

Private string _sql;

///

/// Property: Database query statement

///

Private string_er;

Public string ER

{

Get {return_er;}

}

Public String SQL

{

Get {return _sql;}

Set {_sql = value;

}

///

/// Constructor

///

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

///

Public void db_open ()

{

IF (Conn.State == ConnectionsState.Closed)

{

Conn.connectionstring = system.configuration.configurationSettings.AppSettings ["constr"]. Tostring ();

Cn.open ();

}

}

///

/// Turn off the database connection

///

Public void db_close ()

{

IF (Conn.State == ConnectionsState.Open) {

CONN.CLOSE ();

Conn.dispose ();

}

}

///

/ / / Perform a non-return type query statement

///

///

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

///

/// public sqldatareader executeReader ()

{

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;

}

}

///

///

///

///

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

///

///

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;

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-67065.html

New Post(0)