SqlHelper

SqlHelper.cs带事务

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Text;

namespace PDAFramework
{
    public class SqlHelper : IDisposable
    {
        //连接字符串
        string conn_str;
        //sql连接对象
        SqlConnection conn;
        //事务
        System.Data.SqlClient.SqlTransaction trans;

        #region SqlHelper 构造函数
        public SqlHelper()
        {
            conn_str = ConfigurationManager.ConnectionStrings["sqlconnstr"].ToString();
            conn = new SqlConnection(conn_str);
            conn.Open();
        }
        #endregion

        #region BeginTran开始事务 Commit提交事务 RollBack回滚事务
        /// <summary>
        /// 开始事务
        /// </summary>
        public void BeginTran()
        {
            trans = conn.BeginTransaction();
        }
        /// <summary>
        /// 提交事务
        /// </summary>
        public void Commit()
        {
            trans.Commit();
        }
        /// <summary>
        /// 回滚事务
        /// </summary>
        public void RollBack()
        {
            trans.Rollback();
        }
        #endregion

        #region ExcuteReader 返回SqlDataReader
        /// <summary>
        /// 返回SqlDataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public SqlDataReader ExcuteReader(string sql, params SqlParameter[] pams)
        {
            SqlCommand comm = new SqlCommand(sql, conn);
            if (pams != null && pams.Length > 0)
            {
                for (int i = 0; i < pams.Length; i++)
                {
                    comm.Parameters.Add(pams[i]);
                }
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return comm.ExecuteReader();

        }
        #endregion

        #region ExcuteDataTable 返回DataTable
        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public DataTable ExcuteDataTable(string sql, params SqlParameter[] pams)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conn_str))
            {
                if (pams != null && pams.Length > 0)
                {
                    adapter.SelectCommand.Parameters.AddRange(pams);
                }
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                return dt;
            }
        }
        #endregion

        #region ExcuteNonQuery 返回受影响行数
        /// <summary>
        /// 返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public int ExcuteNonQuery(string sql, params SqlParameter[] pams)
        {
            SqlCommand comm = new SqlCommand(sql, conn);
            if (pams != null && pams.Length > 0)
            {
                comm.Parameters.AddRange(pams);
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return comm.ExecuteNonQuery();

        }
        #endregion

        #region ExcuteScalar 返回结果中的第一行第一列
        /// <summary>
        /// 返回结果中的第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public object ExcuteScalar(string sql, params SqlParameter[] pams)
        {
            SqlCommand comm = new SqlCommand(sql, conn);

            if (pams != null && pams.Length > 0)
            {
                comm.Parameters.AddRange(pams);
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return comm.ExecuteScalar();
        }
        #endregion

        #region IDisposable 成员 用using回收资源
        public void Dispose()
        {
            if (trans != null)
            {
                trans.Dispose();
            }
            if (conn != null)
            {
                conn.Dispose();
            }
        }
        #endregion
    }
}
View Code

SqlHelper.cs精简版不带事务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace WpfApplication1
{
    static class SqlHelper
    {
        public readonly static string connStr = ConfigurationManager.ConnectionStrings["myconnStr"].ConnectionString;

        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }

        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    if (parameters != null)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(cmd);
                    apdater.Fill(dataset);
                    return  dataset.Tables[0];
                }
            }
        }
    }
}
View Code

 

 

你可能感兴趣的:(SqlHelper)