国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.Net 通用數據操作類 (附通用數據基類)第1/2頁

2024-07-10 13:22:07
字體:
來源:轉載
供稿:網友
文章內容為本站編輯,創作.你可以任意轉載、發布、使用但請務必以明文標注文章原始出處及本聲明   作者:浪淘沙
此貼的方法會持續更新, 此文件要引用與數據操作的基類

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;

namespace EC
{
    /// <summary>
    /// EB通用與數據交互操作基類
    /// </summary>
    public class EBCommonObj:IDisposable
    {
        private bool _alreadyDispose = false;
        private DBOperate dbo;
        private string sql = null;
        private System.Data.DataSet ds;

        #region 構造與析構函數
        public EBCommonObj()
        {
            dbo = new DBOperate();
        }

        ~EBCommonObj()
        {
            dbo.Dispose();
            Dispose();
        }
        protected virtual void Dispose(bool isDisposing)
        {
            if (_alreadyDispose) return;
            if (isDisposing)
            {
                dbo.Dispose();
            }
            _alreadyDispose = true;
        }
        #endregion

        #region IDisposable 成員
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

        #region 通用刪除數據庫中的某條記錄
        /// <summary>
        /// 通用刪除數據庫中的某條記錄
        /// </summary>
        /// <param name="tbl">數據表名</param>
        /// <param name="fld">字段名</param>
        /// <param name="IsInt">是否是int型</param>
        /// <param name="kev">關鍵詞值</param>
        public void CommDelByID(string tbl, string fld, bool IsInt, string key)
        {
            sql = "delete from {0} where {1}=";
            if (IsInt)
            {
                sql += "{3}";
            }
            else
            {
                sql += "'{3}'";
            }
            dbo.ExecuteNonQuery(string.Format(sql, tbl, fld, IsInt, key));

        }

        #endregion

        #region 通用讀取數據庫中的某條記錄
        /// <summary>
        /// 通用讀取數據庫中的某條記錄
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="fld"></param>
        /// <param name="IsInt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public DataSet CommReadByID(string tbl,string fld,bool IsInt,string key)
        {
            sql = "select * from {0} where {1}=";
            if (IsInt)
            {
                sql += "{3}";
            }
            else
            {
                sql += "'{3}'";
            }
            ds = dbo.GetDataSet(string.Format(sql, tbl, fld, IsInt, key));

            return ds;        
        }
        #endregion

        #region 修改數據庫中的某條記錄為true 或flase
        /// <summary>
        /// 修改數據庫中的某條記錄為true 或flase
        /// </summary>
        /// <param name="tbl">表格式</param>
        /// <param name="fld">主鍵標識</param>
        /// <param name="Isint">是否整形</param>
        /// <param name="key">主鍵</param>
        /// <param name="flgfld">flase鍵</param>
        /// <param name="flgkey">key值</param>
        public void CommUpdateByID(string tbl,string fld,bool Isint,string key,string flgfld,int flgkey)
        {

            sql = "update {0} set {4}={5} where {1}=";
            if (Isint)
            {
                sql += "{3}";
            }
            else
            {
                sql += "'{3}'";
            }
            dbo.ExecuteNonQuery(string.Format(sql, tbl, fld, Isint, key, flgfld, flgkey));
        }
        #endregion

        #region 綁定DropDown 列表

        /// <summary>
        /// 綁定DropDown 列表
        /// </summary>
        /// <param name="tbl">表名</param>
        /// <param name="selValue">下拉框值</param>
        /// <param name="selText">下拉框顯示內容</param>
        /// <param name="strWhere">where 條件語句 不用加where 沒有條件則為空</param>
        /// <param name="dr">DropDownList控件名稱</param>
        public void DropBind(string tbl, string selValue, string selText, string strWhere,System.Web.UI.WebControls.DropDownList dr)
        {
            ds = GetDrop(tbl, selValue, selText, strWhere);
            dr.DataSource = ds;

            dr.DataTextField = selText;
            dr.DataValueField = selValue;
            dr.DataBind();
            ds.Clear();
            ds.Dispose();
        }

        /// <summary>
        /// 讀取表中數據
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="selValue"></param>
        /// <param name="selText"></param>
        /// <param name="strWhere">條件</param>
        /// <returns></returns>
        public DataSet GetDrop(string tbl,string selValue,string selText,string strWhere)
        {
            sql = "select {1},{2} from {0} where 1=1 and {3}";
            ds = dbo.GetDataSet(string.Format(sql, tbl, selValue, selText, strWhere));
            return ds;        
        }       

        #endregion

        #region 判斷是否有數據
        /// <summary>
        /// 判斷是否有數據:存在數據時返回true,否則返回Flash
        /// </summary>
        /// <param name="tbl">數據表名</param>
        /// <param name="fld">字段名</param>
        /// <param name="key">關鍵詞</param>
        /// <param name="IsKeyInt">是否是數字類型:是:true;否:false</param>
        /// <returns>true或false</returns>
        public bool IsHaveDate(string tbl,string fld,string key,bool IsKeyInt)
        {
            bool Rev = false;
            if (IsKeyInt)
            {
                sql = "select * from {0} where {1}={2}";
            }
            else
            {
                sql = "select * from {0} where {1}='{2}'";
            }
            ds = dbo.GetDataSet(string.Format(sql, tbl, fld, key));
            if (ds.Tables[0].Rows.Count > 0)
            {
                Rev = true;
            }
            return Rev;
        }

        #endregion
    }
}






/############################################
版權聲明:
文章內容為本站編輯,創作.你可以任意轉載、發布、使用但請務必標明文章原始出處及本聲明
  作者:浪淘沙
############################################/

/**********************************************************************************
 * 
 * 功能說明:數據操作基類,可以執行內聯SQL語句和存儲過程
 * 作者: 劉功勛;
 * 版本:V0.1(C#2.0);時間:2006-4-28
 * 
 * *******************************************************************************/
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace EC
{
    /// <summary>
    /// 數據庫連接及操作對象類
    /// </summary>
    public class DBBase
    {
        private bool _alreadyDispose = false;
        private System.Data.SqlClient.SqlConnection conn;
        private System.Data.SqlClient.SqlCommand com;

        #region 構造與柝構
        public DBBase()
        {
            try
            {
                conn=new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                conn.Open();
                com = new System.Data.SqlClient.SqlCommand();
                com.Connection = conn;
            }
            catch (Exception ee)
            {
                throw new Exception("連接數據庫出錯");
            }
        }
        ~DBBase()
        {
            Dispose();
        }
        protected virtual void Dispose(bool isDisposing)
        {
            if (_alreadyDispose) return;
            if (isDisposing)
            {
                // TODO: 此處釋放受控資源 
                if (com != null)
                {
                    com.Cancel();
                    com.Dispose();
                }
                if (conn != null)
                {
                    try
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                    catch (Exception ee)
                    {
                    }
                    finally
                    {
                        conn = null;
                    }
                }
            }
            // TODO: 此處釋放非受控資源。設置被處理過標記 
            _alreadyDispose = true;
        }
        #endregion
        #region IDisposable 成員

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        #region 數據基本操作
        /// <summary>
        /// ExecuteNonQuery
        /// </summary>
        /// <param name="sqlString">SQL語句</param>
        /// <returns>返回影響行數</returns>
        public int ExecuteNonQuery(string sqlString)
        {
            int ret = 0;
            com.CommandText = sqlString;
            com.CommandType = CommandType.Text;
            try
            {
                ret = com.ExecuteNonQuery();
            }
            catch (Exception ee)
            {
                throw new Exception("SQL:" + sqlString + "<br />" + ee.Message.ToString());
            }
            finally
            {
                com.Cancel();
            }
            return ret;
        }
        /// <summary>
        /// 執行插入語句返回IDENTITY
        /// </summary>
        /// <param name="sqlString">SQL語句</param>
        /// <returns>@@IDENTITY</returns>
        public int ExecInsert(string sqlString)
        {
            int identity = 0;
            //僅能執行Insert into 語句
            if (!sqlString.ToLower().Contains("insert into"))
            {
                return -1;
            }
            sqlString += " Select @@IDENTITY";
            System.Data.DataSet ds = new DataSet();
            try
            {
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlString, conn);
                da.Fill(ds);
                da.Dispose();
            }
            catch (Exception ee)
            {
                throw new Exception("SQL:" + sqlString + "<br />" + ee.Message.ToString());
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                identity =Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            ds.Clear();
            ds.Dispose();
            return identity;
        }
        /// <summary>
        /// 執行SQL語句返回記錄集
        /// </summary>
        /// <param name="sqlString">SQL語句</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet(string sqlString)
        {
            System.Data.DataSet ds = new DataSet();
            try
            {
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlString, conn);
                da.Fill(ds);
                da.Dispose();
            }
            catch (Exception ee)
            {
                throw new Exception("SQL:" + sqlString + "<br />" + ee.Message.ToString());
            }
            return ds;
        }
        /// <summary>
        /// 執行存儲過程(返回N種參數)
        /// </summary>
        /// <param name="procName">過程名</param>
        /// <param name="hashtable">傳入的參數表</param>
        /// <param name="hashtable1">傳出的參數表</param>
        /// <returns>返回參數表</returns>

        public System.Collections.Hashtable ExecProcedure(string procName, System.Collections.Hashtable hashtable, System.Collections.Hashtable hashtable1)
        {
            System.Collections.Hashtable hashtable2 = new System.Collections.Hashtable();
            System.Collections.IDictionaryEnumerator ide = hashtable.GetEnumerator();
            System.Collections.IDictionaryEnumerator ide1 = hashtable1.GetEnumerator();

            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = procName;

            while (ide.MoveNext())
            {
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide.Key.ToString(), ide.Value);
                com.Parameters.Add(p);
            }
            while (ide1.MoveNext())
            {
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide1.Key.ToString(), ide.Value);
                com.Parameters.Add(p);
            }

            try
            {
                com.ExecuteNonQuery();
                ide1 = hashtable1.GetEnumerator();
                while (ide1.MoveNext())
                {
                    string k = ide1.Key.ToString();
                    hashtable2.Add(k, com.Parameters[k].Value);
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
            finally
            {
                com.Cancel();
            }
            return hashtable2;
        }

        /// <summary>
        /// 執行存儲過程(返回記錄集)
        /// </summary>
        /// <param name="procName">過程名</param>
        /// <param name="hashtable">傳入的參數表</param>
        /// <returns>返回記錄集</returns>
        public DataSet ExecProcedure(string procName, System.Collections.Hashtable hashtable)
        {
            System.Data.DataSet ds = new DataSet();
            com.CommandText = procName;
            com.CommandType = CommandType.StoredProcedure;

            System.Collections.IDictionaryEnumerator ide = hashtable.GetEnumerator();

            while (ide.MoveNext())
            {
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide.Key.ToString(), ide.Value);
                com.Parameters.Add(p);
            }
            try
            {
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
                da.Fill(ds);
                da.Dispose();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
            finally
            {
                com.Cancel();
            }
            return ds;
        }
        #endregion

        #region 數據操作
        /// <summary>
        /// 統計某表記錄總數
        /// </summary>
        /// <param name="KeyField">主鍵/索引鍵</param>
        /// <param name="TableName">數據庫.用戶名.表名</param>
        /// <param name="Condition">查詢條件</param>
        /// <returns>返回記錄總數</returns>
        public int GetRecordCount(string keyField, string tableName, string condition)
        {
            int RecordCount = 0;
            string sql = "select count(" + keyField + ") as count from " + tableName + " " + condition;
            System.Data.DataSet ds = GetDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                RecordCount =Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            ds.Clear();
            ds.Dispose();
            return RecordCount;
        }
        /// <summary>
        /// 統計某表記錄總數
        /// </summary>
        /// <param name="Field">可重復的字段</param>
        /// <param name="tableName">數據庫.用戶名.表名</param>
        /// <param name="condition">查詢條件</param>
        /// <param name="flag">字段是否主鍵</param>
        /// <returns>返回記錄總數</returns>
        public int GetRecordCount(string Field, string tableName, string condition, bool flag)
        {
            int RecordCount = 0;
            if (flag)
            {
                RecordCount = GetRecordCount(Field, tableName, condition);
            }
            else
            {
                string sql = "select count(distinct(" + Field + ")) as count from " + tableName + " " + condition;
                System.Data.DataSet ds = GetDataSet(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                }
                ds.Clear();
                ds.Dispose();
            }
            return RecordCount;
        }
        /// <summary>
        /// 統計某表分頁總數
        /// </summary>
        /// <param name="keyField">主鍵/索引鍵</param>
        /// <param name="tableName">表名</param>
        /// <param name="condition">查詢條件</param>
        /// <param name="pageSize">頁寬</param>
        /// <param name="RecordCount">記錄總數</param>
        /// <returns>返回分頁總數</returns>
        public int GetPageCount(string keyField, string tableName, string condition, int pageSize, int RecordCount)
        {
            int PageCount = 0;
            PageCount = (RecordCount % pageSize) > 0 ? (RecordCount / pageSize) + 1 : RecordCount / pageSize;
            if (PageCount < 1) PageCount = 1;
            return PageCount;
        }
        /// <summary>
        /// 統計某表分頁總數
        /// </summary>
        /// <param name="keyField">主鍵/索引鍵</param>
        /// <param name="tableName">表名</param>
        /// <param name="condition">查詢條件</param>
        /// <param name="pageSize">頁寬</param>
        /// <returns>返回頁面總數</returns>
        public int GetPageCount(string keyField, string tableName, string condition, int pageSize, ref int RecordCount)
        {
            RecordCount = GetRecordCount(keyField, tableName, condition);
            return GetPageCount(keyField, tableName, condition, pageSize, RecordCount);
        }
        /// <summary>
        /// 統計某表分頁總數
        /// </summary>
        /// <param name="Field">可重復的字段</param>
        /// <param name="tableName">表名</param>
        /// <param name="condition">查詢條件</param>
        /// <param name="pageSize">頁寬</param>
        /// <param name="flag">是否主鍵</param>
        /// <returns>返回頁頁總數</returns>
        public int GetPageCount(string Field, string tableName, string condition, ref int RecordCount, int pageSize, bool flag)
        {
            RecordCount = GetRecordCount(Field, tableName, condition, flag);
            return GetPageCount(Field, tableName, condition, pageSize, ref RecordCount);
        }
        #endregion

        #region 分頁函數
         /// <summary>
        /// 構造分頁查詢SQL語句
        /// </summary>
        /// <param name="KeyField">主鍵</param>
        /// <param name="FieldStr">所有需要查詢的字段(field1,field2...)</param>
        /// <param name="TableName">庫名.擁有者.表名</param>
        /// <param name="Condition">查詢條件1(where ...)</param>
        /// <param name="Condition2">查詢條件2(order by ...)</param>
        /// <param name="CurrentPage">當前頁號</param>
        /// <param name="PageSize">頁寬</param>
        /// <returns>SQL語句</returns>
        public static string JoinPageSQL(string KeyField, string FieldStr, string TableName, string Condition, string Condition2, int CurrentPage, int PageSize)
        {
            string sql = null;
            if (CurrentPage == 1)
            {
                sql = "select top " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Condition + " " + Condition2 + " ";
            }
            else
            {
                sql = "select * from (";
                sql += "select top " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Condition + " " + Condition2 + ") a ";
                sql += "where " + KeyField + " not in (";
                sql += "select top " + (CurrentPage - 1) * PageSize + " " + KeyField + " from " + TableName + " " + Condition + " " + Condition2 + ")";
            }
            return sql;
        }
        /// <summary>
        /// 構造分頁查詢SQL語句
        /// </summary>
        /// <param name="Field">字段名(非主鍵)</param>
        /// <param name="TableName">庫名.擁有者.表名</param>
        /// <param name="Condition">查詢條件1(where ...)</param>
        /// <param name="Condition2">查詢條件2(order by ...)</param>
        /// <param name="CurrentPage">當前頁號</param>
        /// <param name="PageSize">頁寬</param>
        /// <returns>SQL語句</returns>
        public static string JoinPageSQL(string Field, string TableName, string Condition, string Condition2, int CurrentPage, int PageSize)
        {
            string sql = null;
            if (CurrentPage == 1)
            {
                sql = "select top " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field;
            }
            else
            {
                sql = "select * from (";
                sql += "select top " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field + " ) a ";
                sql += "where " + Field + " not in (";
                sql += "select top " + (CurrentPage - 1) * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field + ")";
            }
            return sql;
        }
        /// <summary>
        /// 頁面分頁顯示功能
        /// </summary>
        /// <param name="Parameters">參數串(a=1&b=2...)</param>
        /// <param name="RecordCount">記錄總數</param>
        /// <param name="PageSize">頁寬</param>
        /// <param name="CurrentPage">當前頁號</param>
        /// <param name="ShowJump">是否顯示跳轉輸入框及按鈕</param>
        /// <param name="Style">樣式(1:上頁下頁...,2:1234...)</param>
        /// <returns></returns>
        public static string Paging(string Parameters, int RecordCount, int PageCount, int PageSize, int CurrentPage, bool ShowJump, int Style)
        {
            string str;
            if (RecordCount <= PageSize) return "";
            if (Parameters != "") Parameters += "&";
            if (CurrentPage < 1) CurrentPage = 1;
            if (CurrentPage > PageCount) CurrentPage = PageCount;

            str = "<table align='center'  width=/"100%/"><tr><td align=/"center/">";

            str += "共 " + RecordCount + " 條記錄 頁次:" + CurrentPage + "http://m.survivalescaperooms.com/" + PageCount + "頁 ";
            str += PageSize + "條/頁 ";

            if (Style == 1)
            {
                if (CurrentPage == 1)
                    str += "<font color=/"#999999/">首頁 上頁</font> ";
                else
                {
                    str += "<a href='?" + Parameters + "page=1'  class=/"link/">首頁</a> ";
                    str += "<a href='?" + Parameters + "page=" + (CurrentPage - 1) + "'  class=/"link/">上頁</a> "; ;
                }
                if (CurrentPage == PageCount )
                {
                    str += "<font color=/"#999999/">下頁 尾頁</font> ";
                }
                else
                {
                    str += "<a href='?" + Parameters + "page=" + (CurrentPage + 1) + "'  class=/"link/">下頁</a> ";
                    str += "<a href='?" + Parameters + "page=" + PageCount + "'  class=/"link/">尾頁</a>  ";
                }
            }
            else if (Style == 2)
            {
                int NumberSize = 10;
                int PageNumber = (CurrentPage - 1) / NumberSize;

                if (PageNumber * NumberSize > 0)
                    str += "<a href='?" + Parameters + "page=" + PageNumber * NumberSize + "' title=上十頁 >[<<]</a>   ";
                int i;
                for (i = PageNumber * NumberSize + 1; i <= (PageNumber + 1) * NumberSize; i++)
                {
                    if (i == CurrentPage)
                        str += "<strong><font color=#ff0000>[" + i + "]</font></strong> ";
                    else
                        str += "<a href='?" + Parameters + "page=" + i + "'>[" + i + "]</a> ";
                    if (i == PageCount) break;
                }
                if (i < RecordCount) str += "<a href='?" + Parameters + "page=" + i + "' title=下十頁>[>>]</a>  ";
            }
            if (ShowJump)
            {
                str += "";
            }
            str += "</td></tr></table>";
            return str;
        }

        #endregion
    }
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁津县| 宜昌市| 都匀市| 陕西省| 上栗县| 安顺市| 河池市| 临泽县| 峨眉山市| 财经| 宁波市| 玛曲县| 正蓝旗| 沙湾县| 阆中市| 临沧市| 龙州县| 彝良县| 咸宁市| 项城市| 张家港市| 黎城县| 马公市| 道孚县| 霍城县| 西吉县| 清水河县| 鄯善县| 天全县| 吉水县| 靖宇县| 崇文区| 泸定县| 海盐县| 交城县| 安乡县| 珲春市| 中西区| 昂仁县| 德化县| 汾阳市|