sqlhelper【LH】

public class SqlHelper
{
public static string LogConnectionString = ConfigurationManager.ConnectionStrings["LogDB"].ToString();
public static string MasdbConnectionString = ConfigurationManager.ConnectionStrings["MASDB"].ToString();
/// <summary>
/// 构造函数
/// </summary>
static SqlHelper()
{
//var im = new Impersonator(ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["DomainName"],
//ConfigurationManager.AppSettings["Password"]);
}
/// <summary>
/// Execute DataSet
/// </summary>
/// <param name="connectionString">连接数据库的字符串</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="commandParameters">Command Parameters</param>
/// <returns>return DataSet</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{

using (var cmd = new SqlCommand())
{
try
{
using (var conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
var sda = new SqlDataAdapter(cmd);
var ds = new DataSet();
sda.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
}
catch (Exception ex)
{

throw;
}
}
}
/// <summary>
/// Execute DataSet
/// </summary>
/// <param name="connectionString">连接数据库的字符串</param>
/// <param name="cmdType">Command type</param>
/// <param name="sqlCommand">Command Text</param>
/// <returns>return DataSet</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, SqlCommand sqlCommand)
{
try
{
using (var conn = new SqlConnection(connectionString))
{
PrepareCommand(sqlCommand, conn, null, cmdType);
var sda = new SqlDataAdapter(sqlCommand);
var ds = new DataSet();
sda.Fill(ds);
sqlCommand.Parameters.Clear();
sda.Dispose();
return ds;
}
}
catch (Exception)
{
throw;
}
finally
{
if (sqlCommand != null) sqlCommand.Dispose();
}

}
/// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="connectionString">连接数据库的字符串</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="commandParameters">Command Parameters</param>
/// <returns>return int</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="connection">SqlConnection object</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="commandParameters">Command Parameters</param>
/// <returns>return int</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="trans">SqlTransaction object</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="commandParameters">Command Parameters</param>
/// <returns>return int</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// ExecuteReader
/// </summary>
/// <param name="connectionString">连接数据库的字符串</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="readerCallback">reader Callback</param>
/// <param name="commandParameters">Command Parameters</param>
public static void ExecuteReader(string connectionString, CommandType cmdType, string cmdText, Action<SqlDataReader> readerCallback, params SqlParameter[] commandParameters)
{
using (SqlCommand cmd = new SqlCommand())
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
cmd.Parameters.Clear();
readerCallback.Invoke(rdr);
}
}
catch (SqlException ex)
{
throw ex;
}
}
}
}
/// <summary>
/// ExecuteScalar
/// </summary>
/// <param name="connectionString">连接数据库字符串</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="commandParameters">Command Parameters</param>
/// <returns>return object</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
using (SqlCommand cmd = new SqlCommand())
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
}
/// <summary>
/// ExecuteScalar
/// </summary>
/// <param name="connection">SqlConnection object</param>
/// <param name="cmdType">Command Type</param>
/// <param name="cmdText">Command Text</param>
/// <param name="commandParameters">Command Parameters</param>
/// <returns>return object</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{

using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}

/// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">SqlCommand object</param>
/// <param name="conn">SqlConnection object</param>
/// <param name="trans">SqlTransaction object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
/// <param name="cmdParms">SqlParameters to use in the command</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{

if (conn.State != ConnectionState.Open)
conn.Open();

cmd.Connection = conn;
cmd.CommandText = cmdText;

if (trans != null)
cmd.Transaction = trans;

cmd.CommandType = cmdType;

if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">SqlCommand object</param>
/// <param name="conn">SqlConnection object</param>
/// <param name="trans">SqlTransaction object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType)
{

if (conn.State != ConnectionState.Open)
conn.Open();

cmd.Connection = conn;

if (trans != null)
cmd.Transaction = trans;

cmd.CommandType = cmdType;

}
}

你可能感兴趣的:(help)