C#数据库操作类

 /// <summary>
    /// 返回数据库连接字符串
    /// </summary>
    /// <returns></returns>
    public static String GetSqlConnection()
    {
        System.Web.UI.Page page = new System.Web.UI.Page();
        String conn = ConfigurationSettings.AppSettings["OleDbConnectionString"].ToString() + page.Server.MapPath("~/" + ConfigurationSettings.AppSettings["AccessDbPath"].ToString());
        return conn;
    }

    /// <summary>
    ///  获得参数对象
    /// </summary>
    /// <param name="paramName">参数名称</param>
    /// <param name="paramType">数据类型</param>
    /// <param name="paramSize">长度</param>
    /// <param name="ColName">源列名称</param>
    /// <param name="paramValue">参数实值</param>
    /// <returns></returns>
    public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Int32 paramSize, String ColName, Object paramValue)
    {
        OleDbParameter param = new OleDbParameter(paramName, paramType, paramSize, ColName);
        param.Value = paramValue;
        return param;
    }

    /// <summary>
    /// 获得参数对象
    /// </summary>
    /// <param name="paramName">参数名称</param>
    /// <param name="paramType">数据类型</param>
    /// <param name="paramSize">长度</param>
    /// <param name="ColName">源列名称</param>
    /// <returns></returns>
    public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Int32 paramSize, String ColName)
    {
        OleDbParameter param = new OleDbParameter(paramName, paramType, paramSize, ColName);
        return param;
    }

    /// <summary>
    /// 获得参数对象
    /// </summary>
    /// <param name="paramName">参数名称</param>
    /// <param name="paramType">数据类型</param>
    /// <param name="paramSize">长度</param>
    /// <param name="ColName">源列名称</param>
    /// <returns></returns>
    public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Object paramValue)
    {
        OleDbParameter param = new OleDbParameter(paramName,paramType);
        param.Value = paramValue;
        return param;
    }

    /// <summary>
    /// 执行SQL语句
    /// </summary>
    /// <param name="Sqlstr">SQL语句</param>
    /// <param name="param">参数对象数组</param>
    /// <returns></returns>
    public static int ExecuteSql(String Sqlstr, OleDbParameter[] param)
    {
        String ConnStr = OleDbHelper.GetSqlConnection();
        using (OleDbConnection conn = new OleDbConnection(ConnStr))
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = Sqlstr;
            cmd.Parameters.AddRange(param);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            return 1;
        }
    }

    /// <summary>
    /// 执行SQL语句并返回数据表
    /// </summary>
    /// <param name="Sqlstr">SQL语句</param>
    /// <returns></returns>
    public static DataTable ExecuteDt(String Sqlstr)
    {
        String ConnStr = OleDbHelper.GetSqlConnection();
        using (OleDbConnection conn = new OleDbConnection(ConnStr))
        {
            OleDbDataAdapter da = new OleDbDataAdapter(Sqlstr, conn);
            DataTable dt = new DataTable();
            conn.Open();
            da.Fill(dt);
            conn.Close();
            return dt;
        }
    }

    /// <summary>
    /// 执行SQL语句并返回数据表
    /// </summary>
    /// <param name="Sqlstr">SQL语句</param>
    /// <param name="param">参数对象列表</param>
    /// <returns></returns>
    public static DataTable ExecuteDt(String Sqlstr, OleDbParameter[] param)
    {
        String ConnStr = OleDbHelper.GetSqlConnection();
        using (OleDbConnection conn = new OleDbConnection(ConnStr))
        {
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();
            OleDbCommand cmd = new OleDbCommand(Sqlstr,conn);
            cmd.Connection = conn;
            cmd.Parameters.AddRange(param);
            da.SelectCommand = cmd;
            conn.Open();
            da.Fill(dt);
            conn.Close();
            return dt;
        }
    }

    /// <summary>
    /// 批量执行SQL语句
    /// </summary>
    /// <param name="Sqlstr">SQL语句数组</param>
    /// <param name="param">SQL参数对象数组</param>
    /// <returns></returns>
    public static Int32 ExecuteSqls(String [] Sqlstr,List<OleDbParameter []> param)
    {
        String ConnStr = OleDbHelper.GetSqlConnection();
        using (OleDbConnection conn = new OleDbConnection(ConnStr))
        {
           
            OleDbCommand cmd = new OleDbCommand();
            OleDbTransaction tran = null;
            cmd.Transaction = tran;
            try
            {
                conn.Open();
                tran = conn.BeginTransaction();
                cmd.Connection = conn;
                cmd.Transaction = tran;

                Int32 count = Sqlstr.Length;
                for (Int32 i = 0; i < count; i ++ )
                {
                    cmd.CommandText = Sqlstr[i];
                    cmd.Parameters.AddRange(param[i]);
                    cmd.ExecuteNonQuery();
                }
                tran.Commit();
                return 1;
            }
            catch
            {
                tran.Rollback();
                return 0;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值