I accumulated database operation class (ASP.NET)

xiaoxiao2021-04-04  260

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.Web;

Using

System.data.sqlclient;

Using

System.data;

Using

System.configuration;

Namespace

Op_db

{/ ** ////

/// Dedicated to handle operation /// public class db_class {member #REGION member 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;}} #endregion function #REGON function / ** //// /// construct Function /// public db_class () {// // Todo: Add constructor logic // conn.connectionstring = system.configuration.configurationSettings.AppSettings ["constr"]. Tostring ();} / ** //// /// Destructor /// ~ db_class () {i (conn.state == connectionState.open) conn.close ();} / ** //// /// Open Database Connection /// public void db_open () {try {i (conn.state == connectionState.closed) {conn.connectionstring = System.Configuration.ConfigurationSettings.AppSettings [ "constr"] ToString ();. conn.Open ();}} catch (Exception ex) {//System.Web.HttpContext.Current.Response.Redirect(System.Configuration. ConfigurationSettings.appsettings ["conn_admin"]. TOSTRING ());

System.Web.httpContext.current.response.write (ex. web.httpContext.current.response.end ();}} / ** ////

/// Turn off Database connection / // public void db_close () {if (conn.state == connectionState.Open) {conn.close (); conn.dispose ();}} / ** //// // / Execute Non-return query 语 /// Does not perform success public bool executesql () {sqlcommand cmd = new sqlcommand (_SQL, conn); this.db_open (); Try {cmd.executenonury (); this.db_close (); return true;} catch (exception ex) {this.db_close (); system.web.httpContext.current.response.write (ex.Message); System.Web .HttpContext.current.response.end (); returnaf false;} } / ** //// /// Execute the database query /// /// DataReader object

}}} Catch {_er = "Statement error"; return null;}} / ** ////

/// Return the SQL statement to DataView /// /// DataView object 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_cl OSE ();}} / ** //// /// will only return a recorded SQL statement to execute and return results /// /// If The default value of the words without recording /// return result is String public string SQL2STR (String Def) {String RR = ""; SQLCommand cmd = new sqlcommand (_SQL, conn); system .DATA.SQLCLIENT.SQLDATAREADER DR; this.db_open (); try {dr = cmd.executeReader (Commandbehavior.CloseConnection);

IF (Dr.hasRows) {Dr.Read (); rr = DR [0] .tostring (); Dr.close (); this.db_close ();}} catch {rr = ";" finally {this. DB_Close ();} if (rr == ") RR = Def; RETURN RR;} / ** ////

// / /////// //////// executed successfully 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;}} #ENDREGON}} http://guanvee.cnblogs.com/archive/2006/06/16/427510.html

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

New Post(0)