C#中 应用事物的SqlHelper

    using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.Generic;

namespace DLL
{
    public class SQLHelper
    {
        //连接数据源
        private static SqlConnection myConnection = null;
        private static readonly string connectionString = ConfigurationManager.ConnectionStrings["scm"].ConnectionString;


        //打开数据库连接.
        private static void Open()
        {
            if (myConnection == null)
            {
                myConnection = new SqlConnection(connectionString);
            }
            if (myConnection.State == ConnectionState.Closed)
            {
                try
                {
                    myConnection.Open();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
        }
        //关闭数据库连接
        public static void Close()
        {
            if (myConnection != null)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
            }
        }

        //释放资源
        public static void Dispose()
        {
            if (myConnection != null)
            {
                myConnection.Dispose();
                myConnection = null;
            }
        }
        //用于调用事物
        public static int RunProc(List<ProcList> list)
        {
            int re = 0;
            Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = myConnection;
            SqlTransaction trans = myConnection.BeginTransaction();
            try
            {
                cmd.Transaction = trans;

                for (int i = 0; i < list.Count; i++)
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = list[i].procName;
                    cmd.Parameters.Clear();
                    if (list[i].prams != null)
                    {
                        foreach (SqlParameter parameter in list[i].prams)
                        {
                            if (parameter != null)
                            {
                                cmd.Parameters.Add(parameter);
                            }


                        }
                    }

                    re = cmd.ExecuteNonQuery();
                }
                trans.Commit();
                return re;
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
               
            }
            finally
            {
                Close();
            }
           
        }
        //用于增删改
        public static int RunProc(string procName, SqlParameter[] prams)
        {
            int count = 0;
            Open();
            SqlCommand cmd = new SqlCommand(procName, myConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlTransaction trans = myConnection.BeginTransaction();
            cmd.Transaction = trans;
            //判断是否为null
            if (prams != null)
            {
                foreach (SqlParameter item in prams)
                {
                    //判断其子项是否为null
                    if (item != null)
                    {
                        cmd.Parameters.Add(item);
                    }

                }
            }
            try
            {
                count = cmd.ExecuteNonQuery();
                trans.Commit();
                return count;
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                Close();
            }
        }
        // 返回DataSet
        public static DataSet GetDataSet(string procName, SqlParameter[] prams)
        {
            DataSet ds = new DataSet();
            try
            {
                myConnection = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(procName, myConnection);
                cmd.CommandType = CommandType.StoredProcedure;
                if (prams != null)//判断是否有参数
                {
                    foreach (SqlParameter item in prams)
                    {
                        if (item != null)
                        {
                            cmd.Parameters.Add(item);
                        }

                    }

                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);

            }
            catch (Exception ex)
            {
                throw ex;
            }
          
            return ds;
        }
        //用Sql脚本的
        public static DataSet RunSQL(string sql, SqlParameter[] prams)
        {
            Open();
            DataSet ds = new DataSet();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                if (prams != null)
                {
                    foreach (SqlParameter item in prams)
                    {
                        if (item != null)
                        {
                            cmd.Parameters.Add(item);
                        }

                    }
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);

            }
            catch (Exception ex)
            {
                throw ex;
            }
          
            return ds;

        }
        //用Sql脚本2
        public static DataSet RunSQL(string sql)
        {
            Open();
            DataSet ds = new DataSet();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
          
            return ds;

        }

        // 这个用于分页的调用,返回output总页数,和总条数
        public static DataSet GetDataSetAndOutput(string proName, SqlParameter[] para, out int pageCount, out int Counts)
        {
            DataSet ds = new DataSet();
            try
            {
                myConnection = new SqlConnection(connectionString);
                SqlDataAdapter da = new SqlDataAdapter(proName, myConnection);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (para != null)
                {
                    foreach (SqlParameter parameter in para)
                    {
                        if (parameter != null)
                        {
                            da.SelectCommand.Parameters.Add(parameter);
                        }

                    }
                }
                da.SelectCommand.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;
                da.SelectCommand.Parameters.Add("@Counts", SqlDbType.Int).Direction = ParameterDirection.Output;
                da.Fill(ds);
                pageCount = (int)da.SelectCommand.Parameters["@pageCount"].Value;
                Counts = (int)da.SelectCommand.Parameters["@Counts"].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }

    }
}

阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭