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 /// summary> public class db_class {member #REGION member 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;}} #endregion function #REGON function / ** //// /// construct Function /// summary> public db_class () {// // Todo: Add constructor logic // conn.connectionstring = system.configuration.configurationSettings.AppSettings ["constr"]. Tostring ();} / ** //// /// Destructor /// summary> ~ db_class () {i (conn.state == connectionState.open) conn.close ();} / ** //// /// Open Database Connection /// summary> 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 / // summary> public void db_close () {if (conn.state == connectionState.Open) {conn.close (); conn.dispose ();}} / ** //// // / Execute Non-return query 语 summary> /// Does not perform success returns> 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 /// summary> /// DataReader object returnRead 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;}} / ** //// /// Return the SQL statement to DataView /// summary> /// DataView object returns> 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 /// summary> /// If The default value of the words without recording param> /// return result is String returns> 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;} / ** //// // / /////// summary> //////// executed successfully returns> 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