Share an instance of an ASP. NET database Operation class

Source: Internet
Author: User
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&LT;/RETURNS&GT    ;    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 &LT;/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 (); }      }    }  }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.