程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> c# 數據庫的 sql 參數封裝類的編寫

c# 數據庫的 sql 參數封裝類的編寫

編輯:C#基礎知識

數據庫的 sql 參數封裝類的編寫
代碼如下:

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;
using System.Text;
namespace ChinaSite.classes
{
    public class DbAccess
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public DbAccess()
        {
            //
            // TODO: 在此處添加構造函數邏輯
            //
            conn = new SqlConnection();
            //conn.ConnectionString = "initial catalog=pubs;data source=.;user id=sa;password=";
            //conn.ConnectionString = Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["datasource"]);
            conn.ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["datasource"]);
            cmd = new SqlCommand();
            cmd.Connection = conn;
        }
        /// <summary>
        /// 獲取數據根據sql語句 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable GetTable(string sql)
        {
            DataSet ds = new DataSet();

            try
            {
                cmd.CommandText = sql;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return null;

            }
            return ds.Tables[0] ?? new DataTable();
        }

        /// <summary>
        /// 獲取數據根據sql語句 帶參數 的 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public DataTable GetTable(string sql, params SqlParameter[] pas)
        {
            DataSet ds = new DataSet();
            try
            {
                cmd.CommandText = sql;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                cmd.Parameters.Clear();

                foreach (SqlParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }


                da.Fill(ds);
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return null;
            }
            return ds.Tables[0] ?? new DataTable();
        }
        /// <summary>
        /// 根據sql語句返回跟新狀態
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool GetState(string sql)
        {
            bool succ = false;
            try
            {
                cmd.CommandText = sql;
                conn.Open();
                succ = cmd.ExecuteNonQuery() > 0 ? (true) : (false);
                conn.Close();
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return false;
            }
            return succ;

        }
        /// <summary>
        /// 根據sql語句返回跟新狀態帶參數的 
        /// </summary>
        /// <param name="sql">sql語句</param>
        /// <param name="pas">參數的集合</param>
        /// <returns></returns>
        public bool GetState(string sql, params SqlParameter[] pas)
        {
            bool succ = false;
            try
            {
                cmd.CommandText = sql;
                cmd.Parameters.Clear();

                foreach (SqlParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                conn.Open();
                succ = cmd.ExecuteNonQuery() > 0 ? (true) : (false);
                conn.Close();
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return false;
            }
            return succ;

        }
        /// <summary>
        /// 根據sql語句返回第一個單元格的數據
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string GetOne(string sql)
        {
            string res = "";
            try
            {
                cmd.CommandText = sql;
                conn.Open();
                res = cmd.ExecuteScalar() == null ? ("") : (Convert.ToString(cmd.ExecuteScalar()));
                conn.Close();
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return null;
            }
            return res;
        }
        /// <summary>
        ///  根據sql語句返回第一個單元格的數據帶參數的 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public string GetOne(string sql, params SqlParameter[] pas)
        {
            string res = "";
            try
            {
                cmd.CommandText = sql;
                cmd.Parameters.Clear();

                foreach (SqlParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                conn.Open();
                res = cmd.ExecuteScalar() == null ? ("") : (Convert.ToString(cmd.ExecuteScalar()));
                conn.Close();
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return null;
            }
            return res;
        }
        /// <summary>
        /// 返回數據的DataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public SqlDataReader GetDataReader(string sql)
        {
            SqlDataReader dr = null;
            try
            {
                conn.Open();
                cmd.CommandText = sql;
                dr = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return null;
            }
            return dr;
        }
        /// <summary>
        /// 返回數據的DataReader帶參數的 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public SqlDataReader GetDataReader(string sql, params SqlParameter[] pas)
        {
            SqlDataReader dr = null;
            try
            {
                conn.Open();
                cmd.Parameters.Clear();

                foreach (SqlParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }

                cmd.CommandText = sql;
                dr = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {

                this.ShowError(ex.Message);
                return null;
            }
            return dr;
        }
        /// <summary>
        /// 打開連接
        /// </summary>
        public void OpenConn()
        {
            if (conn.State != ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {

                    this.ShowError(ex.Message);
                    return;
                }
            }
        }
        /// <summary>
        /// 關閉連接
        /// </summary>
        public void CloseConn()
        {
            if (conn.State != ConnectionState.Closed)
            {
                try
                {
                    conn.Close();
                    cmd = null;
                    conn = null;
                }
                catch (Exception ex)
                {

                    this.ShowError(ex.Message);
                    return;
                }
            }
        }
        /// <summary>
        /// 彈出錯誤的信息 
        /// </summary>
        /// <param name="err"></param>
        public void ShowError(string err)
        {
            System.Web.HttpContext.Current.Response.Write(Script(err, ""));
        }
        /// <summary>
        /// 顯示信息 
        /// </summary>
        /// <param name="err"></param>
        public void ShowMessage(string mes, string loc)
        {
            System.Web.HttpContext.Current.Response.Write(Script(mes, loc));
        }
        /// <summary>
        /// javascript腳本
        /// </summary>
        /// <param name="mess"></param>
        /// <param name="loc"></param>
        /// <returns></returns>
        public string Script(string mess, string loc)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language='javascript'>");
            sb.Append("alter('");
            sb.Append(mess);
            sb.Append("');");
            sb.Append(loc);
            sb.Append("</script>");
            return sb.ToString();

        }

    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved