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

首頁 > 開發 > 綜合 > 正文

自己寫的一套處理數據庫的方法

2024-07-21 02:45:02
字體:
來源:轉載
供稿:網友
class COperateAcc
    {
        PRivate SqlConnection conn = null;//連接數據庫
        private SqlCommand comm = null;//操作數據庫sql
        private SqlDataAdapter ada = null;//填充dataset
        private SqlDataReader reader = null;//讀取數據庫表中的值
        
        //構造函數
        //初始化連接數據庫
        public COperateAcc()
        {
            try
            {
                DBCon dbcon = new DBCon();
                conn = dbcon.getConn();


            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                close();
            }
        }
        //讀完數據庫值關閉所有連接
        //通過判斷看所有連接是否關閉,沒關閉的情況關閉
        public void close()
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        //功能:查詢sql語句
        public SqlDataReader executeReader(string str_SqlString)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                comm = new SqlCommand();
                comm.CommandText = str_SqlString;
                comm.Connection = conn;
                conn.Open();
                reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                throw e;
            }
            return reader;
           
        }

        //功能:獲得分頁的行數
        public int getPageRecord(string str_SqlString)
        {

            int count;
            try
            {

                comm = new SqlCommand();
                comm.CommandText = str_SqlString;
                comm.Connection = conn;
                conn.Open();
                count = (int)comm.ExecuteScalar();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                close();
            }
            return count;
        }
        //功能:把從數據庫表中得到的數據保存到客戶端的一張表中
        //返回:dataset 已一張表的形式保存到dataset
        //參數:str_SqlString 查詢的sql語句
        public DataSet getDataSet(string str_SqlString)
        {
            DataSet ds = new DataSet();
            try
            {
                ada = new SqlDataAdapter(str_SqlString, conn);
                ada.Fill(ds);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                close();
            }
            return ds;
        }
        //功能:把從數據庫表中得到的數據保存到客戶端的一張表中
        //返回:datatable 已一張表的形式保存到datatable
        //參數:str_SqlString 查詢的sql語句
        public DataTable getDataTable(string str_SqlString)
        {
            DataTable dt = new DataTable();
            try
            {
                ada = new SqlDataAdapter(str_SqlString, conn);
                ada.Fill(dt);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                close();
            }
            return dt;
        }

        //功能:操作數據庫中的表 添加,刪除,更新
        //返回:int 受影響的行數總數。
        //參數:str_SqlString sql語句
        public int executeSql(string str_SqlString)
        {
            int int_Num = 0;
            try
            {
                comm = new SqlCommand();
                comm.CommandText = str_SqlString;
                comm.Connection = conn;
                conn.Open();
                int_Num = comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                close();
            }
            return int_Num;
        }
        //功能:生成數據庫中最大的唯一編號(掛失表等,總共19位 str_Szf+日期+四位數字)
        //返回:string 最大值
        //參數:@par1:str_Szf 用戶自定義字符串 (前兩位為用戶自己規定+后五位位用戶登錄時的單位編號)
        //     :@par2:str_Id 數據庫唯一編號字段名
        //      :@par3:str_TableName 數據庫表名
        public string getMaxID()
        {
            string str_MaxNum = "";
            try
            {
                string sql = "select max(OId) from OrderRegister where  OId like  '%' ";
                reader = this.executeReader(sql);
                reader.Read();
                if (reader[0].ToString().Equals(""))
                {
                    str_MaxNum = this.getDateZh(this.getDate()) + "01";
                }
                else
                {
                    if (reader[0].ToString().Substring(0, 8).Equals(this.getDateZh(this.getDate())))
                    {
                        str_MaxNum = this.getDateZh(this.getDate()) + Convert.ToString((Convert.ToInt32(reader[0].ToString().Substring(8, 2)) + 1)).PadLeft(2, '0');
                    }
                    else
                    {
                        str_MaxNum = this.getDateZh(this.getDate()) + "01";
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                close();
            }
            return str_MaxNum;
        }
        //得到當天日期(年+月+日)
        public string getDate()
        {
            string date = Convert.ToString(DateTime.Now).Substring(0, Convert.ToString(DateTime.Now).IndexOf(' '));
            return date;
        }
        //轉換日期形式
        public string getDateZh(string str_DateValue)
        {
            string str_DValue;
            if (str_DateValue.Length == 8)//八位 例如2008-1-1
            {
                str_DValue = str_DateValue.Substring(0, 4) + "0" + str_DateValue.Substring(5, 1) + "0" + str_DateValue.Substring(7, 1);
            }
            else if (Convert.ToInt32(str_DateValue.Length) == 9)//九位 分別為:2008-11-6,2008-2-12
            {
                if (str_DateValue.Substring(6, 1) == "-")
                {
                    str_DValue = str_DateValue.Substring(0, 4) + "0" + str_DateValue.Substring(5, 1) + str_DateValue.Substring(7, 2);
                }
                else
                {
                    str_DValue = str_DateValue.Substring(0, 4) + str_DateValue.Substring(5, 2) + "0" + str_DateValue.Substring(8, 1);
                }
            }
            else//十位 例如:2008-11-23
            {
                str_DValue = str_DateValue.Substring(0, 4) + str_DateValue.Substring(5, 2) + str_DateValue.Substring(8, 2);
            }
            return str_DValue;
        }

       
    }

如果是access數據庫 將SqlConnection  SqlCommand 等換成 OleDbConnection OleDbCommand 就可以了。



本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/yangliuyilovexi/archive/2009/12/17/5027382.aspx
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 班戈县| 葵青区| 呈贡县| 池州市| 洪江市| 乐山市| 台湾省| 开原市| 旬邑县| 宣恩县| 东丽区| 鹿邑县| 福鼎市| 鹤峰县| 诸暨市| 舒城县| 尤溪县| 瓮安县| 区。| 勐海县| 邵阳县| 凤阳县| 闻喜县| 巴里| 郓城县| 娄烦县| 伊金霍洛旗| 丘北县| 文成县| 荔浦县| 阳原县| 乐昌市| 敦煌市| 阜平县| 建宁县| 阜平县| 民县| 承德市| 金门县| 惠州市| 西昌市|