Ado.net 通用访问类

public class DbHelperSQL
    {
        private static string connString = ConfigurationManager.ConnectionStrings["SqlDataSource"].ToString();

        #region 获取一个连接通道
        /// <summary>
        /// 获取一个连接通道
        /// </summary>
        /// <returns></returns>
        public static SqlConnection CreateConn()
        {
            return new SqlConnection(connString);
        }
        #endregion

        #region 获取数据源
        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public static DataSet Query(string sql, CommandType cmdType = CommandType.Text, params SqlParameter[] parms)
        {
            DataSet dt = new DataSet();
            using (SqlConnection conn = CreateConn())
            {
                SqlDataAdapter da = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = cmdType;
                if (parms != null)
                {
                    cmd.Parameters.AddRange(parms);
                }
                da.SelectCommand = cmd;
                da.Fill(dt);
            }

            return dt;
        }
        #endregion

        #region 执行事务
        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="sql">存储过程名</param>
        /// <param name="parmsList">每个存储过程中所包含的参数</param>
        /// <returns></returns>
        public static int ExecTran(string sql,  List<SqlParameter[]> parmsList,CommandType cmdType = CommandType.Text)
        {
            int row = 0;
            using (SqlConnection conn = CreateConn())
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand(sql, conn, tran);
                cmd.CommandType = cmdType;
                if (parmsList != null && parmsList.Count>0)
                {
                    foreach (SqlParameter[] parms in parmsList)
                    {
                        cmd.Parameters.Clear();
                        if (parms != null)
                        {
                            cmd.Parameters.AddRange(parms);
                        } 
                        try
                        {
                            row += cmd.ExecuteNonQuery();
                        }
                        catch
                        {
                            tran.Rollback();
                            return 0;
                        }
                    }
                    tran.Commit();
                }
            }
            return row;
        }
        #endregion

        #region 执行包含主表和子表的事务
        /// <summary>
        /// 执行包含主表和子表的事务
        /// </summary>
        /// <param name="MainSql">主表存储过程</param>
        /// <param name="MainParmsList">主表存储过程包含的参数</param>
        /// <param name="ChildSql">子表存储过程</param>
        /// <param name="ChildParmsList"></param>
        /// <returns></returns>
        public int ExecTran(string MainSql, CommandType cmdType, List<SqlParameter[]> MainParmsList, string ChildSql, List<SqlParameter[]> ChildParmsList)
        {
            int row = 0;
            using (SqlConnection conn = CreateConn())
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand(MainSql, conn, tran);               
                cmd.CommandType = cmdType;               
                if (MainParmsList != null)
                {
                    foreach (SqlParameter[] parms in MainParmsList)
                    { 
                        cmd.Parameters.Clear();
                        if (parms != null)
                        {                           
                            cmd.Parameters.AddRange(parms);                           
                        }
                        try
                        {
                            row = cmd.ExecuteNonQuery();
                        }
                        catch
                        {
                            tran.Rollback();
                            return 0;
                        }
                    }
                }
                cmd.CommandText = ChildSql;
                if (ChildParmsList != null)
                {
                    foreach (SqlParameter[] parm in ChildParmsList)
                    {
                        cmd.Parameters.Clear();
                        if (parm != null)
                        {
                            cmd.Parameters.AddRange(parm);
                        }
                        try
                        {
                            row = cmd.ExecuteNonQuery();
                        }
                        catch
                        {
                            tran.Rollback();
                            return 0;
                        }
                    }
                    tran.Commit();
                }
            }
            return row;
        }
        #endregion

        #region 执行Sql语句
        /// <summary>
        /// 执行Sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public static int ExecuteSql(string sql, CommandType cmdType = CommandType.Text, params SqlParameter[] parms)
        {
            int row = 0;
            using (SqlConnection conn = CreateConn())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = cmdType;
                if (parms != null)
                {
                    cmd.Parameters.AddRange(parms);
                }
                row = cmd.ExecuteNonQuery();
            }
            return row;
        }
        #endregion

        #region 获取首行首列
        /// <summary>
        /// 获取首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public static object GetSingle(string sql, CommandType cmdType= CommandType.Text, params SqlParameter[] parms)
        {
            using (SqlConnection conn = CreateConn())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = cmdType;
                if (parms != null)
                {
                    cmd.Parameters.AddRange(parms);
                }
                return cmd.ExecuteScalar();
            }
        }
        #endregion
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。