ASP. NET database operation instance and asp.net database instance

Source: Internet
Author: User

ASP. NET database operation instance and asp.net database instance

This example describes the ASP. NET database operation class. We will share this with you for your reference. The details are 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> // summary of SqlServerDataBase /// </summary> public class SqlServerDataBase {private string strError = null; p Rivate int intCount = 0; public SqlServerDataBase () {// TODO: add the constructor logic here //}/// <summary> /// public method DBConn, return Database connection // </summary> /// <returns> </returns> public SqlConnection DBconn () {string strConn = "Server = (local); Database = GlobalMeetings; uid = sa; pwd = "; try {return new SqlConnection (strConn) ;}catch (Exception) {return null ;}} /// <summary> /// publish the attribute ErrorMessage and return the error message. /// </summary> p Ublic string ErrorMessage {get {return strError ;}} /// <summary> /// retrieve data from the database based on the query statement // </summary> /// <param name = "strSelect"> query statement </param> /// <param name = "SqlConn"> database connection </param> // <returns> If data exists, the DataSet object is returned, otherwise, null </returns> public DataSet Select (string SelectString, SqlConnection sqlConn) {strError = ""; SqlConnection conn; if (sqlConn = null) is returned) {conn = DBconn () ;}else {conn = sqlConn;} Try {// if the current status of the database connection is closed, open the connection 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 the database /// </summary> /// <param name = "UpdateString"> Update SQL statement </param> /// <param name = "SqlConn"> database connection </param> // <returns> returns true after successful Update </returns> public bool Update (string UpdateString, sqlConnection SqlConn) {return udiDataBase (UpdateString, SqlConn );} /// <summary> /// delete data from the database /// </summary> /// <param name = "DeleteString"> Dele Te SQL statement </param> /// <param name = "SqlConn"> database connection </param> /// <returns> returns true If deletion is successful </returns> public bool delete (string DeleteString, sqlConnection SqlConn) {return udiDataBase (DeleteString, SqlConn );} /// <summary> /// Insert data into the database /// </summary> /// <param name = "InsertString"> Insert SQL statement </param> // /<param name = "SqlConn"> database connection </param> // <returns> returns true after successful insertion </returns> public bool Insert (string Inser TString, SqlConnection SqlConn) {return udiDataBase (InsertString, SqlConn );} /// <summary> /// update the database based on the SQL statement // </summary> /// <param name = "UDIString"> Update statement </param> // /<param name = "SqlConn"> database connection </param> // <returns> returns true if the update is successful </returns> public bool udiDataBase (string UDIString, sqlConnection SqlConn) {strError = ""; SqlConnection conn; if (SqlConn = null) {conn = DBconn ();} else {conn = S QlConn;} 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.