|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Configuration;
|
|
|
using System.Data;
|
|
|
using System.Data.Common;
|
|
|
using System.Data.SqlClient;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
|
|
|
namespace CommonFunc
|
|
|
{
|
|
|
public class SqlServerDBHelper
|
|
|
{
|
|
|
//日志
|
|
|
public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
private string dbConnStr;
|
|
|
private SqlConnection dbConn = new SqlConnection();
|
|
|
SqlCommandBuilder sbBuilder = new SqlCommandBuilder();
|
|
|
//数据库连接
|
|
|
public string getDBConnStr()
|
|
|
{
|
|
|
return dbConnStr;
|
|
|
}
|
|
|
|
|
|
public SqlServerDBHelper(string initDBConnStr)
|
|
|
{
|
|
|
if (initDBConnStr == null || initDBConnStr.Length == 0)
|
|
|
{
|
|
|
log.WarnFormat("传入的数据库连接串为空!");
|
|
|
}
|
|
|
dbConnStr = initDBConnStr;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 数据库连接
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public bool connect()
|
|
|
{
|
|
|
bool bSucc = false;
|
|
|
dbConn.Close();
|
|
|
dbConn.ConnectionString = dbConnStr;
|
|
|
try
|
|
|
{
|
|
|
dbConn.Open();
|
|
|
bSucc = true;
|
|
|
log.InfoFormat("数据库连接已打开!");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("连接数据库时发生异常:" + ex.Message);
|
|
|
}
|
|
|
return bSucc;
|
|
|
}
|
|
|
|
|
|
public void close()
|
|
|
{
|
|
|
if (dbConn.State != ConnectionState.Closed)
|
|
|
{
|
|
|
dbConn.Close();
|
|
|
log.InfoFormat("数据库连接已关闭!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public ConnectionState connectionState
|
|
|
{
|
|
|
get { return dbConn.State; }
|
|
|
}
|
|
|
|
|
|
public void CheckConnection()
|
|
|
{
|
|
|
if (dbConn.State == ConnectionState.Closed || dbConn.State == ConnectionState.Broken)
|
|
|
{
|
|
|
connect();
|
|
|
}
|
|
|
else if (dbConn.State == ConnectionState.Connecting || dbConn.State == ConnectionState.Executing || dbConn.State == ConnectionState.Fetching)
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 执行SQL命令 返回DataSet
|
|
|
/// </summary>
|
|
|
/// <param name="sql">sql命令</param>
|
|
|
/// <param name="type">执行类型</param>
|
|
|
/// <param name="sqlparams">参数</param>
|
|
|
/// <returns></returns>
|
|
|
public DataSet getDataSet(string sql, params SqlParameter[] sqlparams)
|
|
|
{
|
|
|
CheckConnection();
|
|
|
DataSet ds = new DataSet();
|
|
|
using (SqlDataAdapter da = new SqlDataAdapter(sql, dbConn))
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (sqlparams != null)
|
|
|
{
|
|
|
da.SelectCommand.Parameters.AddRange(sqlparams);
|
|
|
}
|
|
|
da.Fill(ds);
|
|
|
//防止俩个函数使用同一个sqlparams导致的“另一个SqlParameterCollection中已包含SqlParameter”错误
|
|
|
da.SelectCommand.Parameters.Clear();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("执行查询操作异常:" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
return ds;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 执行sql(insert、delete、update)语句进行非查询操作
|
|
|
/// </summary>
|
|
|
/// <param name="sql">sql命令</param>
|
|
|
/// <returns>返回受影响行数</returns>
|
|
|
public int executeUpdate(string sql)
|
|
|
{
|
|
|
CheckConnection();
|
|
|
int rowCount = 0;
|
|
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, dbConn))
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
rowCount = cmd.ExecuteNonQuery();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("执行更新操作异常:{0}", ex.Message);
|
|
|
}
|
|
|
}
|
|
|
return rowCount;
|
|
|
}
|
|
|
|
|
|
public int executeUpdate(string sql, params SqlParameter[] sqlparams)
|
|
|
{
|
|
|
CheckConnection();
|
|
|
int rowCount = 0;
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, dbConn))
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (sqlparams != null)
|
|
|
{
|
|
|
cmd.Parameters.AddRange(sqlparams);
|
|
|
}
|
|
|
rowCount = cmd.ExecuteNonQuery();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("执行更新操作异常:{0}", ex.Message);
|
|
|
}
|
|
|
}
|
|
|
return rowCount;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 使用SqlDataAdapter.Update()更新数据
|
|
|
/// </summary>
|
|
|
/// <param name="str"></param>
|
|
|
public void updateDataTable(DataTable dt, string selectSql, MissingSchemaAction msaOption = MissingSchemaAction.AddWithKey)
|
|
|
{
|
|
|
CheckConnection();
|
|
|
using (SqlDataAdapter da = new SqlDataAdapter())
|
|
|
{
|
|
|
da.MissingSchemaAction = msaOption;
|
|
|
using (SqlCommand cmd = new SqlCommand(selectSql, dbConn))
|
|
|
{
|
|
|
da.SelectCommand = cmd;
|
|
|
sbBuilder.DataAdapter = da;
|
|
|
da.InsertCommand = da.InsertCommand;
|
|
|
da.UpdateCommand = da.UpdateCommand;
|
|
|
da.DeleteCommand = da.DeleteCommand;
|
|
|
try
|
|
|
{
|
|
|
da.Update(dt);
|
|
|
dt.AcceptChanges();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("数据更新时发生异常{0},SQL:{1}", ex.Message, selectSql);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 执行多条SQL语句,实现数据库事务。
|
|
|
/// </summary>sql2000数据库
|
|
|
/// <param name="sqlList">多条SQL语句</param>
|
|
|
public bool executeBatchSql(List<string> sqlList)
|
|
|
{
|
|
|
CheckConnection();
|
|
|
bool isCommit = false;
|
|
|
string currSql = string.Empty;
|
|
|
SqlTransaction tx = dbConn.BeginTransaction();
|
|
|
SqlCommand cmd = new SqlCommand();
|
|
|
try
|
|
|
{
|
|
|
cmd.Connection = dbConn;
|
|
|
cmd.Transaction = tx;
|
|
|
for (int iLoop = 0; iLoop < sqlList.Count; iLoop++)
|
|
|
{
|
|
|
isCommit = false;
|
|
|
currSql = sqlList[iLoop].Trim();
|
|
|
if (currSql.Length > 0)
|
|
|
{
|
|
|
cmd.CommandText = currSql;
|
|
|
cmd.ExecuteNonQuery();
|
|
|
}
|
|
|
}
|
|
|
tx.Commit();
|
|
|
isCommit = true;
|
|
|
}
|
|
|
catch (System.Data.SqlClient.SqlException E)
|
|
|
{
|
|
|
tx.Rollback();
|
|
|
log.WarnFormat("执行事务过程中出现异常:{0},{1}", E.Message, currSql);
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
cmd.Dispose();
|
|
|
}
|
|
|
return isCommit;
|
|
|
}
|
|
|
|
|
|
public object getScalar(string sql, params SqlParameter[] sqlparams)
|
|
|
{
|
|
|
CheckConnection();
|
|
|
Object scaleValue = null;
|
|
|
if (dbConn.State != ConnectionState.Open)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
DataSet ds = new DataSet();
|
|
|
using (SqlDataAdapter da = new SqlDataAdapter(sql, dbConn))
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (sqlparams != null)
|
|
|
{
|
|
|
da.SelectCommand.Parameters.AddRange(sqlparams);
|
|
|
}
|
|
|
da.Fill(ds);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("执行查询操作异常{0},SQL:{1},{2}", ex.Message, sql, sqlparams);
|
|
|
}
|
|
|
}
|
|
|
try
|
|
|
{
|
|
|
if (ds.Tables[0].Rows.Count > 0)
|
|
|
{
|
|
|
scaleValue = ds.Tables[0].Rows[0][0];
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.WarnFormat("获取标量值发生异常{0}", ex.Message);
|
|
|
}
|
|
|
return scaleValue;
|
|
|
}
|
|
|
}
|
|
|
}
|