自己写的一套处理数据库的方法

 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 就可以了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值