分享一個ASP.NET資料庫操作類的執行個體

來源:互聯網
上載者:User
這篇文章主要介紹了ASP.NET資料庫操作類,結合執行個體形式分析了asp.net基本的資料庫增刪改查操作封裝相關技巧,需要的朋友可以參考下

本文執行個體講述了ASP.NET資料庫操作類。分享給大家供大家參考,具體如下:

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 的摘要說明  /// </summary>  public class SqlServerDataBase  {    private string strError = null;    private int intCount = 0;    public SqlServerDataBase()    {      //      // TODO: 在此處添加建構函式邏輯      //    }    /// <summary>    /// 公開方法DBConn,返回資料庫連接    /// </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>    /// 公開屬性ErrorMessage,返回錯誤資訊    /// </summary>    public string ErrorMessage    {      get      {        return strError;      }    }    /// <summary>    /// 根據查詢語句從資料庫檢索資料    /// </summary>    /// <param name="strSelect">查詢語句</param>    /// <param name="SqlConn">資料庫連接</param>    /// <returns>有資料則返回DataSet對象,否則返回null</returns>    public DataSet Select(string SelectString, SqlConnection sqlConn)    {      strError = "";      SqlConnection conn;      if (sqlConn == null)      {        conn = DBconn();      }      else      {        conn = sqlConn;      }      try      {        //若資料庫連接的目前狀態是關閉的,則開啟串連        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 = "資料檢索失敗:" + e.Message;        return null;      }      finally      {        if (conn.State != ConnectionState.Closed)        {          conn.Close();        }      }    }    /// <summary>    /// 更新資料庫    /// </summary>    /// <param name="UpdateString">Update Sql語句</param>    /// <param name="SqlConn">資料庫連接</param>    /// <returns>更新成功返回true</returns>    public bool Update(string UpdateString, SqlConnection SqlConn)    {      return udiDataBase(UpdateString, SqlConn);    }    /// <summary>    /// 從資料庫中刪除資料    /// </summary>    /// <param name="DeleteString">Delete Sql語句</param>    /// <param name="SqlConn">資料庫連接</param>    /// <returns>刪除成功返回true</returns>    public bool Delete(string DeleteString, SqlConnection SqlConn)    {      return udiDataBase(DeleteString, SqlConn);    }    /// <summary>    /// 把資料插入資料庫    /// </summary>    /// <param name="InsertString">Insert Sql語句</param>    /// <param name="SqlConn">資料庫連接</param>    /// <returns>插入成功返回true</returns>    public bool Insert(string InsertString, SqlConnection SqlConn)    {      return udiDataBase(InsertString, SqlConn);    }    /// <summary>    /// 根據Sql語句更新資料庫    /// </summary>    /// <param name="UDIString">更新語句</param>    /// <param name="SqlConn">資料庫連接</param>    /// <returns>更新成功則返回true</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 = "更新資料庫失敗:" + e.Message;        return false;      }      finally      {        if (conn.State != ConnectionState.Closed)        {          conn.Close();        }      }    }  }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.