Asp.net database operation class instance

Time:2021-12-1

This paper describes the ASP. Net database operation class with an example. Share with you 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>
  ///Summary description of sqlserverdatabase
  /// </summary>
  public class SqlServerDataBase
  {
    private string strError = null;
    private int intCount = 0;
    public SqlServerDataBase()
    {
      //
      //Todo: add constructor logic here
      //
    }
    /// <summary>
    ///Expose the method dbconn and return the 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>
    ///Expose the property ErrorMessage and return error information
    /// </summary>
    public string ErrorMessage
    {
      get
      {
        return strError;
      }
    }
    /// <summary>
    ///Retrieve data from the database according to the query statement
    /// </summary>
    ///< param name = "strselect" > query statement < / param >
    ///< param name = "sqlconn" > database connection < / param >
    ///< returns > if there is data, the dataset object is returned; otherwise, null < / returns >
    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, the connection is opened
        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 SQL statement < / param >
    ///< param name = "sqlconn" > database connection < / param >
    ///< returns > the update succeeds and returns true < / returns >
    public bool Update(string UpdateString, SqlConnection SqlConn)
    {
      return udiDataBase(UpdateString, SqlConn);
    }
    /// <summary>
    ///Delete data from database
    /// </summary>
    ///< param name = "deletestring" > delete SQL statement < / param >
    ///< param name = "sqlconn" > database connection < / param >
    ///< returns > the deletion succeeds and returns true < / returns >
    public bool Delete(string DeleteString, SqlConnection SqlConn)
    {
      return udiDataBase(DeleteString, SqlConn);
    }
    /// <summary>
    ///Insert data into database
    /// </summary>
    ///< param name = "insertstring" > insert SQL statement < / param >
    ///< param name = "sqlconn" > database connection < / param >
    ///< returns > insert successfully returns 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 >
    ///< param name = "sqlconn" > database connection < / param >
    ///< 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 = 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();
        }
      }
    }
  }
}

For more information about asp.net, readers who are interested can see the special topics on this site:《Summary of asp.net optimization skills》、《Summary of asp.net string operation skills》、《Summary of asp.net operating XML skills》、《Summary of asp.net file operation skills》、《Asp.net Ajax skills summary topic》And《Summary of asp.net cache operation skills》。

I hope this article will help you in ASP. Net programming.