SqlServer数据库SqlHelper

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helper
{
    public class SqlHelper
    {
        private string connStr = string.Empty;

        public SqlHelper(string connStr)
        {
            this.connStr = connStr;
        }

        public SqlHelper(string ip, string dataBase, string user, string password)
        {
            this.connStr = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", ip, dataBase, user, password);
        }

        /// 
        /// 创建连接对象
        /// 
        /// 
        public SqlConnection CreateConnection()
        {
            var conn = new SqlConnection(connStr);
            conn.Open();
            return conn;
        }

        /// 
        /// 创建连接对象
        /// 
        /// 连接字符串
        /// 
        public static SqlConnection CreateConnection(string connStr)
        {
            var conn = new SqlConnection(connStr);
            conn.Open();
            return conn;
        }

        /// 
        /// 执行数据库sql,返回影响行数
        /// 
        /// 执行的sql语句
        /// 
        /// 查询参数
        /// 
        public int ExecuteNonQuery(string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
        {
            using (var conn = CreateConnection())
            using (var cmd = conn.CreateCommand())
            {
                if (conn.State == System.Data.ConnectionState.Broken || conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.CommandText = cmdStr;
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = outTime;
                if (paras != null && paras.Length > 0)
                {
                    cmd.Parameters.Add(paras);
                }
                return cmd.ExecuteNonQuery();
            }
        }

        /// 
        /// 执行数据库sql,返回首行首列单元格内容
        /// 
        /// 
        /// 
        /// 
        /// 
        public object ExecuteScalar(string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
        {
            using (var conn = CreateConnection())
            using (var cmd = conn.CreateCommand())
            {
                if (conn.State == System.Data.ConnectionState.Broken || conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.CommandText = cmdStr;
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = outTime;
                if (paras != null && paras.Length > 0)
                {
                    cmd.Parameters.AddRange(paras);
                }
                return cmd.ExecuteScalar();
            }
        }

        /// 
        /// 执行sql查询,返回数据表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public List ExecuteQuery(string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
        {
            var dataSet = new DataSet();
            var result = new List();
            using (var conn = CreateConnection())
            using (var cmd = new SqlCommand(cmdStr, conn))
            {
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = outTime;
                if (paras != null && paras.Length > 0)
                {
                    cmd.Parameters.AddRange(paras);
                }
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(dataSet);
                }
            }

            if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count > 0)
            {
                for (int i = 0; i < dataSet.Tables.Count; i++)
                {
                    result.Add(dataSet.Tables[i]);
                }
            }
            return result;
        }

        /// 
        /// 执行数据库sql,返回影响行数
        /// 
        /// 执行的sql语句
        /// 
        /// 查询参数
        /// 
        public static int ExecuteNonQuery(string connStr,string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
        {
            using (var conn = CreateConnection(connStr))
            using (var cmd = conn.CreateCommand())
            {
                if (conn.State == System.Data.ConnectionState.Broken || conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.CommandText = cmdStr;
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = outTime;
                if (paras != null && paras.Length > 0)
                {
                    cmd.Parameters.Add(paras);
                }
                return cmd.ExecuteNonQuery();
            }
        }

        /// 
        /// 执行数据库sql,返回首行首列单元格内容
        /// 
        /// 
        /// 
        /// 
        /// 
        public static object ExecuteScalar(string connStr,string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
        {
            using (var conn = CreateConnection(connStr))
            using (var cmd = conn.CreateCommand())
            {
                if (conn.State == System.Data.ConnectionState.Broken || conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.CommandText = cmdStr;
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = outTime;
                if (paras != null && paras.Length > 0)
                {
                    cmd.Parameters.AddRange(paras);
                }
                return cmd.ExecuteScalar();
            }
        }

        /// 
        /// 执行sql查询,返回数据表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List ExecuteQuery(string connStr,string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
        {
            var dataSet = new DataSet();
            var result = new List();
            using (var conn = CreateConnection(connStr))
            using (var cmd = new SqlCommand(cmdStr, conn))
            {
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = outTime;
                if (paras != null && paras.Length > 0)
                {
                    cmd.Parameters.AddRange(paras);
                }
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(dataSet);
                }
            }

            if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count > 0)
            {
                for (int i = 0; i < dataSet.Tables.Count; i++)
                {
                    result.Add(dataSet.Tables[i]);
                }
            }
            return result;
        }

        /// 
        /// 执行数据库sql,返回影响行数,带有事务
        /// 
        /// 
        /// 
        /// 
        /// 
        public int ExecuteNonQueryWithTranction(string cmdStr, CommandType cmdType = CommandType.Text, params SqlParameter[] paras)
        {
            using (var conn = CreateConnection())
            using (var sqlTransaction = conn.BeginTransaction())
            using (var cmd = conn.CreateCommand())
            {
                var res = -1;
                try
                {
                    cmd.Transaction = sqlTransaction;
                    cmd.CommandText = cmdStr;
                    cmd.CommandType = cmdType;
                    cmd.CommandTimeout = 30000;
                    if (paras != null && paras.Length > 0)
                    {
                        cmd.Parameters.Add(paras);
                    }

                    if (conn.State == System.Data.ConnectionState.Broken || conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    res = cmd.ExecuteNonQuery();
                    sqlTransaction.Commit();
                    return res;
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    return res;
                }
            }
        }

        /// 
        /// 是否可以连接
        /// 
        /// 
        public bool CanConnection()
        {
            try
            {
                using (var conn = CreateConnection())
                {
                    conn.Open();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// 
        /// 是否可以连接
        /// 
        /// 
        /// 
        public static bool CanConnection(string connStr)
        {
            try
            {
                using (var conn = CreateConnection(connStr))
                {
                    //conn.Open();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// 
        /// 获取使用Windows用户登录模式时的连接字符串
        /// 
        /// 
        /// 
        public static string GetConnectionString(string serverIP)
        {
            return string.Format("Data Source={0};Integrated Security=True;", serverIP);
        }

        /// 
        /// 获取数据库连接字符串
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string GetConnectionString(string serverIP, string dbName, string userName, string password)
        {
            return string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", serverIP, dbName, userName, password);
        }
    }
}

你可能感兴趣的:(SqlServer数据库SqlHelper)