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);
}
/// <summary>
/// 创建连接对象
/// </summary>
/// <returns></returns>
public SqlConnection CreateConnection()
{
var conn = new SqlConnection(connStr);
conn.Open();
return conn;
}
/// <summary>
/// 创建连接对象
/// </summary>
/// <param name="connStr">连接字符串</param>
/// <returns></returns>
public static SqlConnection CreateConnection(string connStr)
{
var conn = new SqlConnection(connStr);
conn.Open();
return conn;
}
/// <summary>
/// 执行数据库sql,返回影响行数
/// </summary>
/// <param name="cmdStr">执行的sql语句</param>
/// <param name="cmdType"></param>
/// <param name="paras">查询参数</param>
/// <returns></returns>
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();
}
}
/// <summary>
/// 执行数据库sql,返回首行首列单元格内容
/// </summary>
/// <param name="cmdStr"></param>
/// <param name="cmdType"></param>
/// <param name="paras"></param>
/// <returns></returns>
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();
}
}
/// <summary>
/// 执行sql查询,返回数据表
/// </summary>
/// <param name="cmdStr"></param>
/// <param name="cmdType"></param>
/// <param name="outTime"></param>
/// <param name="paras"></param>
/// <returns></returns>
public List<DataTable> ExecuteQuery(string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
{
var dataSet = new DataSet();
var result = new List<DataTable>();
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;
}
/// <summary>
/// 执行数据库sql,返回影响行数
/// </summary>
/// <param name="cmdStr">执行的sql语句</param>
/// <param name="cmdType"></param>
/// <param name="paras">查询参数</param>
/// <returns></returns>
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();
}
}
/// <summary>
/// 执行数据库sql,返回首行首列单元格内容
/// </summary>
/// <param name="cmdStr"></param>
/// <param name="cmdType"></param>
/// <param name="paras"></param>
/// <returns></returns>
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();
}
}
/// <summary>
/// 执行sql查询,返回数据表
/// </summary>
/// <param name="cmdStr"></param>
/// <param name="cmdType"></param>
/// <param name="outTime"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static List<DataTable> ExecuteQuery(string connStr,string cmdStr, CommandType cmdType = CommandType.Text, int outTime = 60, params SqlParameter[] paras)
{
var dataSet = new DataSet();
var result = new List<DataTable>();
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;
}
/// <summary>
/// 执行数据库sql,返回影响行数,带有事务
/// </summary>
/// <param name="cmdStr"></param>
/// <param name="cmdType"></param>
/// <param name="paras"></param>
/// <returns></returns>
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;
}
}
}
/// <summary>
/// 是否可以连接
/// </summary>
/// <returns></returns>
public bool CanConnection()
{
try
{
using (var conn = CreateConnection())
{
conn.Open();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 是否可以连接
/// </summary>
/// <param name="connStr"></param>
/// <returns></returns>
public static bool CanConnection(string connStr)
{
try
{
using (var conn = CreateConnection(connStr))
{
//conn.Open();
return true;
}
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 获取使用Windows用户登录模式时的连接字符串
/// </summary>
/// <param name="serverIP"></param>
/// <returns></returns>
public static string GetConnectionString(string serverIP)
{
return string.Format("Data Source={0};Integrated Security=True;", serverIP);
}
/// <summary>
/// 获取数据库连接字符串
/// </summary>
/// <param name="serverIP"></param>
/// <param name="dbName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...