没有排序的HashTable
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
namespace DataBase
{
/**//// <summary>
/// C_HashTable
/// </summary>
public class NoSortHashTable : Hashtable
{
private ArrayList keys = new ArrayList();
public NoSortHashTable()
{
}
public override void Add(object key, object value)
{
base.Add(key, value);
keys.Add(key);
}
public override ICollection Keys
{
get
{
return keys;
}
}
public override void Clear()
{
base.Clear();
keys.Clear();
}
public override void Remove(object key)
{
base.Remove(key);
keys.Remove(key);
}
public override IDictionaryEnumerator GetEnumerator()
{
return base.GetEnumerator();
}
public override bool ContainsKey(object key)
{
for (int i = 0; i < keys.Count; i++)
{
if (((SqlParameter[])key).Rank ==1)
{
if (((SqlParameter[])key)[1].SqlValue.ToString() == ((SqlParameter[])keys[i])[1].SqlValue.ToString())
{
return true;
}
}
}
return false;
}
}
}
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
namespace DataBase
{
/**//// <summary>
/// C_HashTable
/// </summary>
public class NoSortHashTable : Hashtable
{
private ArrayList keys = new ArrayList();
public NoSortHashTable()
{
}
public override void Add(object key, object value)
{
base.Add(key, value);
keys.Add(key);
}
public override ICollection Keys
{
get
{
return keys;
}
}
public override void Clear()
{
base.Clear();
keys.Clear();
}
public override void Remove(object key)
{
base.Remove(key);
keys.Remove(key);
}
public override IDictionaryEnumerator GetEnumerator()
{
return base.GetEnumerator();
}
public override bool ContainsKey(object key)
{
for (int i = 0; i < keys.Count; i++)
{
if (((SqlParameter[])key).Rank ==1)
{
if (((SqlParameter[])key)[1].SqlValue.ToString() == ((SqlParameter[])keys[i])[1].SqlValue.ToString())
{
return true;
}
}
}
return false;
}
}
}
数据访问基础类(基于SQLServer)
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
namespace DataBase
{
/**//// <summary>
/// 数据访问基础类(基于SQLServer)
/// </summary>
public class SQLHelper
{
protected static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
public SQLHelper()
{
}
执行简单SQL语句#region 执行简单SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
/// <returns>执行成功,返回true,否则返回false;</returns>
public bool ExecuteSqlTran(ArrayList SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
return true;
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
conn.Close();
conn.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
return false;
}
}
}
/**//// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(SQLString, connection);
adapter.Fill(ds, "ds");
connection.Close();
return ds;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
#endregion
执行带参数的SQL语句#region 执行带参数的SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为SqlParameter[],value是sql语句)</param>
public void ExecuteSqlTran(NoSortHashTable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Value.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Key;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch (SqlException E)
{
trans.Rollback();
conn.Close();
conn.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw E;
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为SqlParameter[],value是sql语句)</param>
public void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Value.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Key;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch(SqlException E)
{
trans.Rollback();
conn.Close();
conn.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw E;
}
}
}
}
/**//// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SQLString, DbParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return myReader;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
#endregion
存储过程操作#region 存储过程操作
/**//// <summary>
/// 执行存储过程;
/// </summary>
/// <param name="storeProcName">存储过程名</param>
/// <param name="parameters">所需要的参数</param>
/// <returns>返回受影响的行数</returns>
public int RunProcedureExecuteSql(string storeProcName, DbParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
connection.Close();
return rows;
}
}
/**//// <summary>
/// 执行存储过程,返回首行首列的值
/// </summary>
/// <param name="storeProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>返回首行首列的值</returns>
public Object RunProcedureGetSingle(string storeProcName, DbParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader RunProcedureGetDataReader(string storedProcName, DbParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataReader returnReader;
SqlCommand cmd = BuildQueryCommand(connection, storedProcName, parameters);
cmd.CommandType = CommandType.StoredProcedure;
returnReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return returnReader;
}
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>DataSet</returns>
public DataSet RunProcedureGetDataSet(string storedProcName, DbParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet);
connection.Close();
sqlDA.SelectCommand.Parameters.Clear();
sqlDA.Dispose();
return dataSet;
}
}
/**//// <summary>
/// 执行多个存储过程,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
public bool RunProcedureTran(Hashtable SQLStringList)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlTransaction trans = connection.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
cmd.Connection = connection;
string storeName = myDE.Value.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Key;
cmd.Transaction = trans;
cmd.CommandText = storeName;
cmd.CommandType = CommandType.StoredProcedure;
if (cmdParms != null)
{
foreach (SqlParameter parameter in cmdParms)
cmd.Parameters.Add(parameter);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
return true;
}
catch(SqlException E)
{
trans.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw E;
return false;
}
}
}
}
/**//// <summary>
/// 执行多个存储过程,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
public bool RunProcedureTran(NoSortHashTable SQLStringList)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlTransaction trans = connection.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (SqlParameter[] myDE in SQLStringList.Keys)
{
cmd.Connection = connection;
string storeName = SQLStringList[myDE].ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE;
cmd.Transaction = trans;
cmd.CommandText = storeName;
cmd.CommandType = CommandType.StoredProcedure;
if (cmdParms != null)
{
foreach (SqlParameter parameter in cmdParms)
cmd.Parameters.Add(parameter);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
return true;
}
catch(SqlException E)
{
trans.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw;
return false;
}
}
}
}
/**//// <summary>
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, DbParameter[] parameters)
{
if (connection.State != ConnectionState.Open)
connection.Open();
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
return command;
}
#endregion
}
}
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
namespace DataBase
{
/**//// <summary>
/// 数据访问基础类(基于SQLServer)
/// </summary>
public class SQLHelper
{
protected static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
public SQLHelper()
{
}
执行简单SQL语句#region 执行简单SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
/// <returns>执行成功,返回true,否则返回false;</returns>
public bool ExecuteSqlTran(ArrayList SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
return true;
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
conn.Close();
conn.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
return false;
}
}
}
/**//// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(SQLString, connection);
adapter.Fill(ds, "ds");
connection.Close();
return ds;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
#endregion
执行带参数的SQL语句#region 执行带参数的SQL语句
/**//// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为SqlParameter[],value是sql语句)</param>
public void ExecuteSqlTran(NoSortHashTable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Value.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Key;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch (SqlException E)
{
trans.Rollback();
conn.Close();
conn.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw E;
}
}
}
}
/**//// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为SqlParameter[],value是sql语句)</param>
public void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Value.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Key;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch(SqlException E)
{
trans.Rollback();
conn.Close();
conn.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw E;
}
}
}
}
/**//// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SQLString, DbParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
/**//// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return myReader;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
/**//// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
}
private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
#endregion
存储过程操作#region 存储过程操作
/**//// <summary>
/// 执行存储过程;
/// </summary>
/// <param name="storeProcName">存储过程名</param>
/// <param name="parameters">所需要的参数</param>
/// <returns>返回受影响的行数</returns>
public int RunProcedureExecuteSql(string storeProcName, DbParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
connection.Close();
return rows;
}
}
/**//// <summary>
/// 执行存储过程,返回首行首列的值
/// </summary>
/// <param name="storeProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>返回首行首列的值</returns>
public Object RunProcedureGetSingle(string storeProcName, DbParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw new Exception(E.Message);
}
}
}
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader RunProcedureGetDataReader(string storedProcName, DbParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataReader returnReader;
SqlCommand cmd = BuildQueryCommand(connection, storedProcName, parameters);
cmd.CommandType = CommandType.StoredProcedure;
returnReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return returnReader;
}
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>DataSet</returns>
public DataSet RunProcedureGetDataSet(string storedProcName, DbParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet);
connection.Close();
sqlDA.SelectCommand.Parameters.Clear();
sqlDA.Dispose();
return dataSet;
}
}
/**//// <summary>
/// 执行多个存储过程,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
public bool RunProcedureTran(Hashtable SQLStringList)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlTransaction trans = connection.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
cmd.Connection = connection;
string storeName = myDE.Value.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Key;
cmd.Transaction = trans;
cmd.CommandText = storeName;
cmd.CommandType = CommandType.StoredProcedure;
if (cmdParms != null)
{
foreach (SqlParameter parameter in cmdParms)
cmd.Parameters.Add(parameter);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
return true;
}
catch(SqlException E)
{
trans.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw E;
return false;
}
}
}
}
/**//// <summary>
/// 执行多个存储过程,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
public bool RunProcedureTran(NoSortHashTable SQLStringList)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlTransaction trans = connection.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (SqlParameter[] myDE in SQLStringList.Keys)
{
cmd.Connection = connection;
string storeName = SQLStringList[myDE].ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE;
cmd.Transaction = trans;
cmd.CommandText = storeName;
cmd.CommandType = CommandType.StoredProcedure;
if (cmdParms != null)
{
foreach (SqlParameter parameter in cmdParms)
cmd.Parameters.Add(parameter);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
return true;
}
catch(SqlException E)
{
trans.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(E.Message);
throw;
return false;
}
}
}
}
/**//// <summary>
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, DbParameter[] parameters)
{
if (connection.State != ConnectionState.Open)
connection.Open();
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
return command;
}
#endregion
}
}