This article mainly introduces the ASP. NET database operation class, combined with instance form analysis of ASP. NET basic database additions and deletions to check the operation of packaging related skills, need a friend to
The examples in this article describe the ASP. NET database Operation class. Share to everyone for your reference, as follows:
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.sqlclient;namespace mysqlserver{//<summary>// Sqlserverdatabase's summary description///</summary> public class Sqlserverdatabase {private string strerror = null; private int intcount = 0; Public Sqlserverdatabase () {/////TODO: Add the constructor logic here///////<summary>//////publicly dbconn, return Database connection///</summary>//<returns></returns> public SqlConnection Dbconn () {string str Conn = "server= (local);D atabase=globalmeetings; Uid=sa;pwd= "; try {return new SqlConnection (strconn); } catch (Exception) {return null; }}////<summary>//Public Property errormessage, return error message////ErrorMessage get {return strerror; }}////<summary>///Retrieve data from database according to query statement///</summary>//<param name= "strselect" > Query statement </ param>//<param name= "sqlconn" > Database connection </param>//<returns> have data to return a DataSet object, otherwise return Null</retur Ns> Public DataSet Select (string selectstring, SqlConnection sqlconn) {strerror = ""; SqlConnection Conn; if (sqlconn = = NULL) {conn = Dbconn (); } else {conn = sqlconn; try {//If the current state of the database connection is closed, open the Connect if (conn. state = = connectionstate.closed) {Conn. Open (); } SqlDataAdapter mysqldataadapter = new SqlDataAdapter (); SqlCommand SelectCommand = new SqlCommand (selectstring, conn); Selectcommand.commandtype = CommandType.Text; Mysqldataadapter.selectcommand = SelectCommand; DataSet MyDS = new DataSet (); Mysqldataadapter.fill (MyDS); return MyDS; } catch (Exception e) {strerror = "Data retrieval failed:" + e.message; return null; } finally {if (conn. state! = connectionstate.closed) {Conn. Close (); }}}///<summary>//Update database///</summary>//<param name= "updatestring" >update S QL statements </param>///<param Name= "sqlconn" > Database connection </param>//<returns> update successful return TRUE</RETURNS> ; public bool Update (string updatestring, SqlConnection sqlconn) {return udidatabase (updatestring, sqlconn); }///<summary>///delete data from database///</summary>//<param name= "deletestring" >delete SQL statements < /param>//<param name= "sqlconn" > Database connection </param>//<returns> Delete successful return true</returns> publ IC bool Delete (string deletestring, SqlConnection sqlconn) {return udidatabase (deletestring, sqlconn); }///<summary>//Insert data into the database///</summary> <param name= "insertstring" >insert SQL statements </param>///<param Name= "sqlconn" > Database connections </param> <returns> Insert successfully returned true</returns> public bool Insert (string insertstring, SqlConnection sqlconn) { Return Udidatabase (insertstring, sqlconn); }///<summary>//Update database according to SQL statement///</summary>/<param name= "udistring" > UPDATE statement </PARAM&G T <param name= "sqlconn" > Database connection </param>//<returns> update is successful returns true</returns> public bool Udida Tabase (String udistring, SqlConnection sqlconn) {strerror = ""; SqlConnection Conn; if (sqlconn = = NULL) {conn = Dbconn (); } else {conn = sqlconn; } try {if (conn. state = = connectionstate.closed) {Conn. Open (); } SqlCommand cmd = new SqlCommand (udistring, conn); Cmd.commandtype = CommandType.Text; intcount = cmd. ExecuteNonQuery (); Return! (intcount < 1); } catch (Exception e) {strerror = "failed to update database:" + e.message; return false; } finally {if (conn. state! = connectionstate.closed) {Conn. Close (); } } } }}