using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using Common.Model;
namespace ZcPeng.PublicLibrary
{
///
/// DataAccess
/// 功能:SQL SERVER数据库访问类。
/// 作者:彭昭成
/// 时间:2018年12月10日
/// 注意:在使用前,需要设置connectionString。
///
public static class DataAccess
{
//静态字段
///
/// 将NText字段的尺寸设定为100M
///
public static int NTextSize = 100000000;
///
/// 默认的SQL SERVER数据库连接字符串,在使用前需要设定
///
public static string connectionString;
///
/// 默认BulkCopy操作的操时时间设定为60秒
///
public static int BulkCopyTimeout = 60;
///
/// 获取SELECT语句返回的数据表
///
/// SELECT语句
/// 返回得到的数据表
/// 返回操作的结果(输出参数)
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetDataTable(string selectCommandText, DataTable dt, out string result)
{
bool bSuccessed = false;
result = "";
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
if (dt == null)
{
bSuccessed = false;
result = "参数错误:dt不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
try
{
conn.Open();
da.SelectCommand.CommandType = CommandType.Text;
da.Fill(dt);
bSuccessed = true;
result = "已经成功的获取了数据表。";
}
catch (Exception e)
{
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
return bSuccessed;
}
///
/// 从指定的服务器获取SELECT语句返回的数据表
///
/// 数据库连接字符串
/// SELECT语句
/// 返回得到的数据表
/// 跟该SQL语句相对应的参数数组
/// 返回操作的结果(输出参数)
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetDataTable(string strConn, string selectCommandText, DataTable dt, SqlParameter[] parameters, out string result)
{
bool bSuccessed = false;
result = "";
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
if (dt == null)
{
bSuccessed = false;
result = "参数错误:dt不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
try
{
conn.Open();
da.SelectCommand.CommandType = CommandType.Text;
if (parameters != null && parameters.Length > 0)
da.SelectCommand.Parameters.AddRange(parameters);
da.Fill(dt);
bSuccessed = true;
result = "已经成功的获取了数据表。";
}
catch (Exception e)
{
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (da != null && da.SelectCommand != null && da.SelectCommand.Parameters.Count > 0)
da.SelectCommand.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
return bSuccessed;
}
///
/// 从指定的多个服务器获取SELECT语句返回的数据表,并合并数据表。
/// 注意:在执行过程中忽略单台服务器的错误;该方法可能会将dt置为null。
///
/// 数据库连接字符串
/// SELECT语句
/// 返回得到的数据表
/// 跟该SQL语句相对应的参数数组
/// 返回操作的结果(输出参数)
/// 返回成功获取到数据的服务器数目。
public static int GetDataTable(List connectionStringList, string selectCommandText, ref DataTable dt, List parameters, out string result)
{
int successedServerCount = 0;
result = "";
if (selectCommandText == "")
{
result = "参数错误:SELECT语句不能为空。";
}
else
{
if (connectionStringList == null || connectionStringList.Count == 0)
{
result = "数据库连接字符串列表不能为空。";
}
else
{
dt = null;
foreach (string connectionString in connectionStringList)
{
DataTable dtTemp = new DataTable();
if (GetDataTable(connectionString, selectCommandText, dtTemp, (parameters != null && parameters.Count > 0) ? parameters.ToArray() : null, out result))
{
if (dtTemp != null)
{
if (DataTableEx.Merge(ref dt, dtTemp))
successedServerCount++;
}
}
}
}
}
return successedServerCount;
}
///
/// 从指定的多个服务器获取SELECT语句返回的数据表,并汇总数据表。
/// 注意:在执行过程中忽略单台服务器的错误;该方法可能会将dt置为null;除了关键列之外,要求其他列都是整型。
///
/// 数据库连接字符串
/// SELECT语句
/// 返回得到的数据表
/// 跟该SQL语句相对应的参数数组
/// 返回操作的结果(输出参数)
/// 关键列名
/// 返回成功获取到数据的服务器数目。
public static int GetDataTable(List connectionStringList, string selectCommandText, ref DataTable dt, List parameters, out string result, string keyColumnName)
{
List keyColumnNames = new List(1);
keyColumnNames.Add(keyColumnName);
return GetDataTable(connectionStringList, selectCommandText, ref dt, parameters, out result, keyColumnNames);
}
///
/// 从指定的多个服务器获取SELECT语句返回的数据表,并汇总数据表。
/// 注意:在执行过程中忽略单台服务器的错误;该方法可能会将dt置为null;除了关键列之外,要求其他列都是整型。
///
/// 数据库连接字符串
/// SELECT语句
/// 返回得到的数据表
/// 跟该SQL语句相对应的参数数组
/// 返回操作的结果(输出参数)
/// 关键列名
/// 返回成功获取到数据的服务器数目。
public static int GetDataTable(List connectionStringList, string selectCommandText, ref DataTable dt, List parameters, out string result, List keyColumnNames)
{
int successedServerCount = 0;
result = "";
if (selectCommandText == "")
{
result = "参数错误:SELECT语句不能为空。";
}
else
{
if (connectionStringList == null || connectionStringList.Count == 0)
{
result = "数据库连接字符串列表不能为空。";
}
else if (keyColumnNames == null || keyColumnNames.Count == 0)
{
result = "关键列列表不能为空。";
}
else
{
dt = null;
foreach (string connectionString in connectionStringList)
{
DataTable dtTemp = new DataTable();
if (GetDataTable(connectionString, selectCommandText, dtTemp, (parameters != null && parameters.Count > 0) ? parameters.ToArray() : null, out result))
{
if (dtTemp != null)
{
if (DataTableEx.Summarize(ref dt, dtTemp, keyColumnNames))
successedServerCount++;
}
}
}
}
}
return successedServerCount;
}
///
/// 获取SELECT语句返回的数据表
///
/// SELECT语句
/// 返回得到的数据表
/// 跟该SQL语句相对应的参数数组
/// 返回操作的结果(输出参数)
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetDataTable(string selectCommandText, DataTable dt, SqlParameter[] parameters, out string result)
{
bool bSuccessed = false;
result = "";
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
if (dt == null)
{
bSuccessed = false;
result = "参数错误:dt不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
try
{
conn.Open();
da.SelectCommand.CommandType = CommandType.Text;
if (parameters != null && parameters.Length > 0)
da.SelectCommand.Parameters.AddRange(parameters);
da.Fill(dt);
bSuccessed = true;
result = "已经成功的获取了数据表。";
}
catch (Exception e)
{
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (da != null && da.SelectCommand != null && da.SelectCommand.Parameters.Count > 0)
da.SelectCommand.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
return bSuccessed;
}
///
/// 获取SELECT语句返回的数据表
///
/// SELECT语句
/// 返回得到的数据表
/// 跟该SQL语句相对应的参数列表
/// 返回操作的结果(输出参数)
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetDataTable(string selectCommandText, DataTable dt, List parameters, out string result)
{
return GetDataTable(selectCommandText, dt, parameters.ToArray(), out result);
}
///
/// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
/// ,SqlConnection conn = null, SqlTransaction trans 输出值
///
/// SELECT语句
/// 返回的数据
/// 返回操作的结果
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetOneValue(string selectCommandText, out object objValue, out string result,SqlConnection conn = null, SqlTransaction trans = null, bool needTransOut = false)
{
bool bSuccessed = false;
objValue = null;
result = "";
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
conn = new SqlConnection(connectionString);
conn.Open();
trans = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand(selectCommandText, conn, trans);
try
{
cmd.CommandType = CommandType.Text;
objValue = cmd.ExecuteScalar();
bSuccessed = true;
result = "已经成功的获取了数据。";
if(!needTransOut)
trans.Commit();
}
catch (Exception e)
{
trans.Rollback();
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (needTransOut)
{
}
else {
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
return bSuccessed;
}
///
/// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
///
/// SELECT语句
/// 命令参数数组
/// 返回的数据
/// 返回操作的结果
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetOneValue(string selectCommandText, SqlParameter[] parameters, out object objValue, out string result)
{
bool bSuccessed = false;
objValue = null;
result = "";
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand(selectCommandText, conn, trans);
try
{
cmd.CommandType = CommandType.Text;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(parameters);
objValue = cmd.ExecuteScalar();
bSuccessed = true;
result = "已经成功的获取了数据。";
trans.Commit();
}
catch (Exception e)
{
trans.Rollback();
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return bSuccessed;
}
///
/// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
///
/// SELECT语句
/// 命令参数数组
/// 返回的数据
/// 返回操作的结果
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetOneValue(string selectCommandText, SqlParameter[] parameters, out object objValue, out string result, out SqlConnection conn, out SqlTransaction trans, bool needTransOut = false)
{
bool bSuccessed = false;
objValue = null;
result = "";
conn = null;
trans = null;
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
conn = new SqlConnection(connectionString);
conn.Open();
trans = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand(selectCommandText, conn, trans);
try
{
cmd.CommandType = CommandType.Text;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(parameters);
objValue = cmd.ExecuteScalar();
bSuccessed = true;
result = "已经成功的获取了数据。";
if (!needTransOut)
trans.Commit();
}
catch (Exception e)
{
trans.Rollback();
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (needTransOut)
{
}
else
{
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
return bSuccessed;
}
///
/// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
///
/// 数据库连接字符串
/// SELECT语句
/// 命令参数数组
/// 返回的数据
/// 返回操作的结果
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetOneValue(string strConn, string selectCommandText, SqlParameter[] parameters, out object objValue, out string result)
{
bool bSuccessed = false;
objValue = null;
result = "";
if (selectCommandText == "")
{
bSuccessed = false;
result = "参数错误:SELECT语句不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(selectCommandText, conn);
try
{
conn.Open();
cmd.CommandType = CommandType.Text;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(parameters);
objValue = cmd.ExecuteScalar();
bSuccessed = true;
result = "已经成功的获取了数据。";
}
catch (Exception e)
{
bSuccessed = false;
result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return bSuccessed;
}
///
/// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
///
/// SELECT语句
/// 命令参数列表
/// 返回的数据
/// 返回操作的结果
/// 返回操作是否成功,如果成功,返回true;否则返回false。
public static bool GetOneValue(string selectCommandText, List parameters, out object objValue, out string result)
{
return GetOneValue(selectCommandText, parameters.ToArray(), out objValue, out result);
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
///
/// SQL语句
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string connectionString,string commandText, out string result)
{
int recordsAffected = 0; //受影响的行数
result = "";
if (commandText == "")
{
result = "参数错误:SQL语句不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(commandText, conn);
try
{
conn.Open();
cmd.CommandType = CommandType.Text;
recordsAffected = cmd.ExecuteNonQuery();
if (recordsAffected > 0)
result = "已经成功的影响了" + recordsAffected + "条记录。";
}
catch (Exception e)
{
result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return recordsAffected;
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
///
/// SQL语句
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand( string commandText, out string result)
{
return ExecuteCommand(connectionString,commandText, out result);
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
///
/// SQL语句
/// 参数列表
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string connectionString,string commandText, List parameters, out string result)
{
int recordsAffected = 0; //受影响的行数
result = "";
if (commandText == "")
{
result = "参数错误:SQL语句不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(commandText, conn);
try
{
conn.Open();
cmd.CommandType = CommandType.Text;
if (parameters != null && parameters.Count > 0)
cmd.Parameters.AddRange(parameters.ToArray());
recordsAffected = cmd.ExecuteNonQuery();
if (recordsAffected > 0)
result = "已经成功的影响了" + recordsAffected + "条记录。";
}
catch (Exception e)
{
result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return recordsAffected;
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
///
/// SQL语句
/// 参数列表
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string commandText, List parameters, out string result)
{
return ExecuteCommand(connectionString,commandText, parameters,out result);
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
///
/// SQL语句
/// 参数数组
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string commandText, SqlParameter[] parameters, out string result)
{
return ExecuteCommand(commandText, new List(parameters), out result);
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
///
/// SQL语句
/// 参数数组
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string connectionString, string commandText, SqlParameter[] parameters, out string result)
{
return ExecuteCommand(connectionString,commandText, new List(parameters), out result);
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
///
/// SQL语句或者存储过程名
/// 参数列表
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string commandText, List parameters, CommandType commandType, out string result)
{
int recordsAffected = 0; //受影响的行数
result = "";
if (commandText == "")
{
result = "参数错误:SQL语句不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(commandText, conn);
try
{
conn.Open();
cmd.CommandType = commandType;
if (parameters != null && parameters.Count > 0)
cmd.Parameters.AddRange(parameters.ToArray());
recordsAffected = cmd.ExecuteNonQuery();
if (recordsAffected > 0)
result = "已经成功的影响了" + recordsAffected + "条记录。";
}
catch (Exception e)
{
result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return recordsAffected;
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
///
/// 数据库连接字符串
/// SQL语句或者存储过程名
/// 参数列表
/// 命令类型
/// 返回操作的结果
/// 返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static int ExecuteCommand(string strConn, string commandText, List parameters, CommandType commandType, out string result)
{
int recordsAffected = 0; //受影响的行数
result = "";
if (commandText == "")
{
result = "参数错误:SQL语句不能为空。";
}
else
{
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(commandText, conn);
try
{
conn.Open();
cmd.CommandType = commandType;
if (parameters != null && parameters.Count > 0)
cmd.Parameters.AddRange(parameters.ToArray());
recordsAffected = cmd.ExecuteNonQuery();
if (recordsAffected > 0)
result = "已经成功的影响了" + recordsAffected + "条记录。";
}
catch (Exception e)
{
result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return recordsAffected;
}
///
/// 执行SQL语句,并返回受影响的行数。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
///
/// 数据库连接字符串列表
/// 命令
/// 是否需要所有服务器都执行成功才提交事务
/// 返回操作的结果
/// 返回在每个数据库上操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。
public static List ExecuteCommand(List connectionStringList, DataAccessCommand dac, bool needAllDone, out string result)
{
result = "";
if (connectionStringList == null || connectionStringList.Count == 0)
{
result = "数据库连接字符串列表为空。";
return null;
}
//定义变量
int serverCount = connectionStringList.Count;
List connList = new List(serverCount);
List transList = new List(serverCount);
List cmdList = new List(serverCount);
List recordsAffectedList = new List(serverCount);
bool transCommit = true; //是否提交事务
//分别在每台服务器上执行命令
for (int i = 0; i < serverCount; i++)
{
connList.Add(null);
transList.Add(null);
cmdList.Add(null);
recordsAffectedList.Add(0);
try
{
if (string.IsNullOrEmpty(connectionStringList[i]))
{
result = string.Format("第{0}个数据库连接字符串为空。", i);
}
else
{
connList[i] = new SqlConnection(connectionStringList[i]);
connList[i].Open();
transList[i] = connList[i].BeginTransaction();
cmdList[i] = new SqlCommand(dac.CommandText, connList[i], transList[i]);
cmdList[i].CommandType = dac.CommandType;
if (dac.ParamList != null && dac.ParamList.Count > 0)
cmdList[i].Parameters.AddRange(dac.ParamList.ToArray());
recordsAffectedList[i] = cmdList[i].ExecuteNonQuery();
}
}
catch (Exception e)
{
result = "在服务器(" + connectionStringList[i] + ")执行数据操作时发生错误。\r\nSQL语句:" + dac.CommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmdList[i] != null && cmdList[i].Parameters.Count > 0)
cmdList[i].Parameters.Clear(); //为了反复利用参数,某个命令对象在用完之后需要清除参数
}
if (needAllDone)
{
if (dac.RecordsAffectedLargeThanZeroIsSuccessful && recordsAffectedList[i] <= 0)
{
transCommit = false;
break;
}
}
}
//提交还是回滚事务
foreach (SqlTransaction trans in transList)
{
if (trans != null)
{
if (transCommit)
trans.Commit();
else
trans.Rollback();
}
}
foreach (SqlConnection conn in connList)
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
return recordsAffectedList;
}
///
/// 在事务内处理一组命令
/// SqlConnection conn1 = null, SqlTransaction trans1 输入参数
///
/// 命令列表
/// 输出参数,如果失败,返回错误提示
/// 返回是否执行成功
public static bool ExecuteBatchCommands(List commands, out string result, SqlConnection conn1 = null, SqlTransaction trans1 = null,bool needCommitNow = true)
{
bool successed = false;
result = "";
SqlConnection conn;
SqlTransaction trans = null;
if (conn1 == null) {
conn = new SqlConnection(connectionString);
}else
{
conn = conn1;
}
//SqlTransaction trans = null;
SqlCommand cmd = new SqlCommand("", conn,trans);
try
{
if (conn1 == null)
{
conn.Open();
}
if (trans1 == null)
{
trans = conn.BeginTransaction();
}
else
{
trans = trans1;
}
cmd.Transaction = trans;
int executedCommands = 0;
int i = 1;
foreach (DataAccessCommand command in commands)
{
cmd.CommandType = command.CommandType;
cmd.CommandText = command.CommandText;
cmd.Parameters.Clear();
if (command.ParamList != null && command.ParamList.Count > 0)
cmd.Parameters.AddRange(command.ParamList.ToArray());
int recordsAffected = cmd.ExecuteNonQuery();
if (recordsAffected <= 0)
result += $"在执行第{i}条返回false。\r\n recordsAffected:" + recordsAffected +"/n ";
i++;
if (command.RecordsAffectedLargeThanZeroIsSuccessful)
{
//命令影响的行数大于0才算成功
if (recordsAffected > 0)
executedCommands++;
else
break;
}
else
executedCommands++;
}
if (needCommitNow)
{
if (executedCommands == commands.Count)
{
trans.Commit();
successed = true;
}
else
trans.Rollback();
}
result += $"需要执行{commands.Count}条 /n";
result += $"共执行了{executedCommands}条 /n";
}
catch (Exception e)
{
result = "在执行数据操作时发生错误。\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
try
{
if (needCommitNow)
{
trans.Rollback();
}
}
catch
{
}
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (needCommitNow)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return successed;
}
///
/// 在事务内处理一组命令
///
/// 数据库连接字符串
/// 命令列表
/// 输出参数,如果失败,返回错误提示
/// 返回是否执行成功
public static bool ExecuteBatchCommands(string strConn, List commands, out string result)
{
bool successed = false;
result = "";
SqlConnection conn = new SqlConnection(strConn);
SqlTransaction trans = null;
SqlCommand cmd = new SqlCommand("", conn);
try
{
conn.Open();
trans = conn.BeginTransaction();
cmd.Transaction = trans;
int executedCommands = 0;
foreach (DataAccessCommand command in commands)
{
cmd.CommandType = command.CommandType;
cmd.CommandText = command.CommandText;
cmd.Parameters.Clear();
if (command.ParamList != null && command.ParamList.Count > 0)
cmd.Parameters.AddRange(command.ParamList.ToArray());
int recordsAffected = cmd.ExecuteNonQuery();
if (command.RecordsAffectedLargeThanZeroIsSuccessful)
{
//命令影响的行数大于0才算成功
if (recordsAffected > 0)
executedCommands++;
else
break;
}
else
executedCommands++;
}
if (executedCommands == commands.Count)
{
trans.Commit();
successed = true;
}
else
trans.Rollback();
}
catch (Exception e)
{
result = "在执行数据操作时发生错误。\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
try
{
trans.Rollback();
}
catch
{
}
}
finally
{
if (cmd != null && cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
return successed;
}
///
/// 在多个服务器上执行一组命令。
/// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
///
/// 数据库连接字符串列表
/// 命令列表
/// 是否需要所有服务器都执行成功才提交事务
/// 返回操作的结果
/// 返回在每个服务器上是否执行成功。
public static List ExecuteBatchCommands(List connectionStringList, List dacList, bool needAllDone, out string result)
{
result = "";
if (connectionStringList == null || connectionStringList.Count == 0)
{
result = "数据库连接字符串列表为空。";
return null;
}
if (dacList == null || dacList.Count == 0)
{
result = "命令列表为空。";
return null;
}
//定义变量
int serverCount = connectionStringList.Count;
List connList = new List(serverCount);
List transList = new List(serverCount);
List cmdList = new List(serverCount);
List successList = new List(serverCount);
List transCommitList = new List(serverCount); //是否在某个服务器提交事务
bool allDone = true; //是否所有的服务器都执行成功
//分别在每台服务器上执行命令
for (int i = 0; i < serverCount; i++)
{
connList.Add(null);
transList.Add(null);
cmdList.Add(null);
successList.Add(false);
transCommitList.Add(false);
int executedCommands = 0;
successList[i] = false;
try
{
if (string.IsNullOrEmpty(connectionStringList[i]))
{
result = string.Format("第{0}个数据库连接字符串为空。", i);
}
else
{
connList[i] = new SqlConnection(connectionStringList[i]);
connList[i].Open();
transList[i] = connList[i].BeginTransaction();
cmdList[i] = new SqlCommand("", connList[i], transList[i]);
foreach (DataAccessCommand dac in dacList)
{
cmdList[i].CommandText = dac.CommandText;
cmdList[i].CommandType = dac.CommandType;
if (dac.ParamList != null && dac.ParamList.Count > 0)
{
cmdList[i].Parameters.Clear();
cmdList[i].Parameters.AddRange(dac.ParamList.ToArray());
}
int recordsAffected = cmdList[i].ExecuteNonQuery();
if (dac.RecordsAffectedLargeThanZeroIsSuccessful)
{
//命令影响的行数大于0才算成功
if (recordsAffected > 0)
executedCommands++;
else
break;
}
else
executedCommands++;
}
if (executedCommands == dacList.Count)
{
transCommitList[i] = true;
successList[i] = true;
}
else
{
transCommitList[i] = false;
successList[i] = false;
}
}
}
catch (Exception e)
{
result = "在服务器(" + connectionStringList[i] + ")执行数据操作时发生错误。\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
}
finally
{
if (cmdList[i] != null && cmdList[i].Parameters.Count > 0)
cmdList[i].Parameters.Clear(); //为了反复利用参数,某个命令对象在用完之后需要清除参数
}
if (needAllDone)
{
if (successList[i] == false)
{
allDone = false;
break;
}
}
}
//提交还是回滚事务
for (int i = 0; i < connList.Count; i++)
{
if (needAllDone && allDone == false)
transCommitList[i] = false;
if (transList[i] != null)
{
if (transCommitList[i])
transList[i].Commit();
else
transList[i].Rollback();
}
if (connList[i] != null && connList[i].State != ConnectionState.Closed)
{
connList[i].Close();
connList[i] = null;
}
}
return successList;
}
///
/// 用BulkCopy的形式将数据表的内容全部保存到数据库中去
///
/// 数据库连接字符串
/// 源数据表
/// 数据库中的目标表名
/// 列映射列表,如果为null或者Count为0,表示使用默认列映射
/// 事务类型
/// 选项
/// 操时时间(单位:秒)
/// 返回保存是否成功
public static bool BulkCopy(string strConn, DataTable dtSrc, string destTableName, List columnMappings, BulkCopyTransType transType, SqlBulkCopyOptions options, int timeout)
{
bool bSuccessed = false;
if (dtSrc != null && dtSrc.Rows != null && dtSrc.Rows.Count > 0 && destTableName != null && destTableName != "" && timeout > 0)
{
SqlConnection conn = null;
SqlTransaction trans = null;
SqlBulkCopy sbc = null;
try
{
//对不同的事务类型使用不同的构造函数
if (transType == BulkCopyTransType.None)
{
sbc = new SqlBulkCopy(strConn, options);
}
else if (transType == BulkCopyTransType.Internal)
{
sbc = new SqlBulkCopy(strConn, options | SqlBulkCopyOptions.UseInternalTransaction);
}
else
{
conn = new SqlConnection(strConn);
conn.Open();
trans = conn.BeginTransaction();
sbc = new SqlBulkCopy(conn, options, trans);
}
//设置操时时间
sbc.BulkCopyTimeout = timeout;
//设置数据库中的目标表名
sbc.DestinationTableName = destTableName;
//设置列映射
if (columnMappings != null && columnMappings.Count > 0)
{
foreach (SqlBulkCopyColumnMapping columnMapping in columnMappings)
{
sbc.ColumnMappings.Add(columnMapping);
}
}
sbc.WriteToServer(dtSrc);
if (trans != null)
trans.Commit();
bSuccessed = true;
}
catch
{
bSuccessed = false;
if (trans != null)
trans.Rollback();
}
finally
{
if (sbc != null)
sbc.Close();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return bSuccessed;
}
///
/// 用BulkCopy的形式将数据表的内容全部保存到数据库中去
///
/// 源数据表
/// 数据库中的目标表名
/// 列映射列表,如果为null或者Count为0,表示使用默认列映射
/// 事务类型
/// 选项
/// 操时时间(单位:秒)
/// 返回保存是否成功
public static bool BulkCopy(DataTable dtSrc, string destTableName, List columnMappings, BulkCopyTransType transType, SqlBulkCopyOptions options, int timeout)
{
bool bSuccessed = false;
if (dtSrc != null && dtSrc.Rows != null && dtSrc.Rows.Count > 0 && destTableName != null && destTableName != "" && timeout > 0)
{
SqlConnection conn = null;
SqlTransaction trans = null;
SqlBulkCopy sbc = null;
try
{
//对不同的事务类型使用不同的构造函数
if (transType == BulkCopyTransType.None)
{
sbc = new SqlBulkCopy(DataAccess.connectionString, options);
}
else if (transType == BulkCopyTransType.Internal)
{
sbc = new SqlBulkCopy(DataAccess.connectionString, options | SqlBulkCopyOptions.UseInternalTransaction);
}
else
{
conn = new SqlConnection(DataAccess.connectionString);
conn.Open();
trans = conn.BeginTransaction();
sbc = new SqlBulkCopy(conn, options, trans);
}
//设置操时时间
sbc.BulkCopyTimeout = timeout;
//设置数据库中的目标表名
sbc.DestinationTableName = destTableName;
//设置列映射
if (columnMappings != null && columnMappings.Count > 0)
{
foreach (SqlBulkCopyColumnMapping columnMapping in columnMappings)
{
sbc.ColumnMappings.Add(columnMapping);
}
}
sbc.WriteToServer(dtSrc);
if (trans != null)
trans.Commit();
bSuccessed = true;
}
catch
{
bSuccessed = false;
if (trans != null)
trans.Rollback();
}
finally
{
if (sbc != null)
sbc.Close();
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
return bSuccessed;
}
//以下是几个BulkCopy的不同重载
public static bool BulkCopy(DataTable dtSrc, string destTableName)
{
return BulkCopy(dtSrc, destTableName, null, BulkCopyTransType.None, SqlBulkCopyOptions.Default, BulkCopyTimeout);
}
public static bool BulkCopy(DataTable dtSrc, string destTableName, List columnMappings)
{
return BulkCopy(dtSrc, destTableName, columnMappings, BulkCopyTransType.None, SqlBulkCopyOptions.Default, BulkCopyTimeout);
}
public static bool BulkCopy(DataTable dtSrc, string destTableName, List columnMappings, BulkCopyTransType transType)
{
return BulkCopy(dtSrc, destTableName, columnMappings, transType, SqlBulkCopyOptions.Default, BulkCopyTimeout);
}
public static bool BulkCopy(DataTable dtSrc, string destTableName, List columnMappings, BulkCopyTransType transType, SqlBulkCopyOptions options)
{
return BulkCopy(dtSrc, destTableName, columnMappings, transType, options, BulkCopyTimeout);
}
///
/// 得到某个表中符合条件的记录数目
///
/// 表名
/// 筛选条件
/// 是否只统计不重复的记录
/// 输出参数,返回错误信息
/// 返回记录数目;如果发生错误,返回-1。
public static int GetRowCount(string tableName, string filter, bool distinct, out string result)
{
int rowCount = -1;
string selectCommandText = "SELECT " + (distinct ? "DISTINCT" : "") + " COUNT(*) FROM [" + tableName + "]";
if (filter != "")
selectCommandText += " WHERE " + filter;
object objValue;
if (DataAccess.GetOneValue(selectCommandText, out objValue, out result))
{
if (!Convert.IsDBNull(objValue))
rowCount = (int)objValue;
}
return rowCount;
}
public static int GetRowCount(string tableName, string filter, out string result)
{
return GetRowCount(tableName, filter, false, out result);
}
public static int GetRowCount(string tableName, string filter)
{
string result;
return GetRowCount(tableName, filter, false, out result);
}
public static int GetRowCount(string tableName)
{
string result;
return GetRowCount(tableName, "", false, out result);
}
///
/// 使用分页存储过程获取符合条件的分页数据表
///
/// 表名
/// 主键字段名
/// 字段列表
/// 过滤条件(不包含WHERE)
/// 分组字段列表(不包含GROUP BY)
/// 排序字段(不包含ORDER BY)
/// 页号
/// 页大小
/// 返回数据表;如果获取失败,返回null。
public static DataTable GetDataTable(string tableName, string primaryKey, string fields, string filter, string group, string sort, int pageNumber, int pageSize,out string result)
{
//查询语句:这里使用分页查询存储过程
string commandText = "EXECUTE SP_Pagination @Tables,@PrimaryKey,@Sort,@CurrentPage,@PageSize,@Fields,@Filter,@Group";
//参数列表
List paramList = new List(5);
SqlParameter p;
p = new SqlParameter("@Tables", SqlDbType.NVarChar, 1000);
p.Value = tableName;
paramList.Add(p);
p = new SqlParameter("@PrimaryKey", SqlDbType.NVarChar, 100);
p.Value = primaryKey;
paramList.Add(p);
p = new SqlParameter("@Sort", SqlDbType.NVarChar, 200);
p.Value = sort;
paramList.Add(p);
p = new SqlParameter("@CurrentPage", SqlDbType.Int, 4);
p.Value = pageNumber;
paramList.Add(p);
p = new SqlParameter("@PageSize", SqlDbType.Int, 4);
p.Value = pageSize;
paramList.Add(p);
p = new SqlParameter("@Fields", SqlDbType.NVarChar, 1000);
p.Value = fields;
paramList.Add(p);
p = new SqlParameter("@Filter", SqlDbType.NVarChar, 1000);
p.Value = filter;
paramList.Add(p);
p = new SqlParameter("@Group", SqlDbType.NVarChar, 1000);
p.Value = group;
paramList.Add(p);
//定义其他变量
//string result;
DataTable dt = new DataTable();
//执行查询
if (!DataAccess.GetDataTable(commandText, dt, paramList, out result))
dt = null;
return dt;
}
///
/// 使用分页存储过程获取符合条件的分页数据表
///
/// 数据库连接字符串
/// 表名
/// 主键字段名
/// 字段列表
/// 过滤条件(不包含WHERE)
/// 分组字段列表(不包含GROUP BY)
/// 排序字段(不包含ORDER BY)
/// 页号
/// 页大小
/// 返回数据表;如果获取失败,返回null。
public static DataTable GetDataTable(string strConn, string tableName, string primaryKey, string fields, string filter, string group, string sort, int pageNumber, int pageSize)
{
//查询语句:这里使用分页查询存储过程
string commandText = "EXECUTE SP_Pagination @Tables,@PrimaryKey,@Sort,@CurrentPage,@PageSize,@Fields,@Filter,@Group";
//参数列表
List paramList = new List(5);
SqlParameter p;
p = new SqlParameter("@Tables", SqlDbType.NVarChar, 1000);
p.Value = tableName;
paramList.Add(p);
p = new SqlParameter("@PrimaryKey", SqlDbType.NVarChar, 100);
p.Value = primaryKey;
paramList.Add(p);
p = new SqlParameter("@Sort", SqlDbType.NVarChar, 200);
p.Value = sort;
paramList.Add(p);
p = new SqlParameter("@CurrentPage", SqlDbType.Int, 4);
p.Value = pageNumber;
paramList.Add(p);
p = new SqlParameter("@PageSize", SqlDbType.Int, 4);
p.Value = pageSize;
paramList.Add(p);
p = new SqlParameter("@Fields", SqlDbType.NVarChar, 1000);
p.Value = fields;
paramList.Add(p);
p = new SqlParameter("@Filter", SqlDbType.NVarChar, 1000);
p.Value = filter;
paramList.Add(p);
p = new SqlParameter("@Group", SqlDbType.NVarChar, 1000);
p.Value = group;
paramList.Add(p);
//定义其他变量
string result;
DataTable dt = new DataTable();
//执行查询
if (!DataAccess.GetDataTable(strConn, commandText, dt, null, out result))
dt = null;
return dt;
}
///
/// 返回参数列表
///
///
public static List ToParameters(List> plist)
{
List parameters = new List(plist.Count);
foreach (List