using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
namespace HealthExamining.DAL {
public static class DBHelper {
public static readonly string ConnectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["HealthExamining"].ConnectionString;
private static SqlCommand CreateCommand(string commandText, SqlConnection con) {
if(con.State != ConnectionState.Open)
con.Open();
return new SqlCommand(commandText, con);
}
public static SqlDataReader ExecuteReader(SqlConnection con, CommandType commandType, string commandText, SqlParameter[] parameters) {
con.StateChange += new StateChangeEventHandler(con_StateChange);
SqlCommand cmd = CreateCommand(commandText, con);
cmd.CommandType = commandType;
if(parameters != null)
cmd.Parameters.AddRange(parameters);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
public static object ExecuteScalar(CommandType commandType, string commandText, SqlParameter[] parameters) {
object obj = null;
using(SqlConnection con = new SqlConnection(ConnectionString)) {
con.StateChange += new StateChangeEventHandler(con_StateChange);
SqlCommand cmd = CreateCommand(commandText, con);
cmd.CommandType = commandType;
if(parameters != null)
cmd.Parameters.AddRange(parameters);
obj = cmd.ExecuteScalar();
}
return obj;
}
public static object ExecuteProcedureReturnValue(string procedureName, SqlParameter[] parameters) {
object obj = null;
using(SqlConnection con = new SqlConnection(ConnectionString)) {
SqlCommand cmd = CreateCommand(procedureName, con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pReturnValue = cmd.CreateParameter();
pReturnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pReturnValue);
if(parameters != null)
cmd.Parameters.AddRange(parameters);
cmd.ExecuteNonQuery();
obj = pReturnValue.Value;
}
return obj;
}
public static void ExecuteNonQuery(string commandText) {
using(SqlConnection con = new SqlConnection(ConnectionString)) {
con.StateChange += new StateChangeEventHandler(con_StateChange);
SqlCommand cmd = CreateCommand(commandText, con);
cmd.ExecuteNonQuery();
}
}
public static void ExecuteNonQuery(CommandType commandType, string commandText, SqlParameter[] parameters) {
using(SqlConnection con = new SqlConnection(ConnectionString)) {
con.StateChange += new StateChangeEventHandler(con_StateChange);
SqlCommand cmd = CreateCommand(commandText, con);
cmd.CommandType = commandType;
if(parameters != null)
for(int i = 0; i < parameters.Length; i++)
cmd.Parameters.Add(parameters[i]);
cmd.ExecuteNonQuery();
}
}
public static void ExecuteNonQueryInTransaction(SqlTransaction trans, CommandType commandType, string commandText, SqlParameter[] parameters) {
SqlConnection con = trans.Connection;
SqlCommand cmd = CreateCommand(commandText, con);
cmd.CommandType = commandType;
cmd.Transaction = trans;
if(parameters != null)
cmd.Parameters.AddRange(parameters);
cmd.ExecuteNonQuery();
}
static void con_StateChange(object sender, StateChangeEventArgs e) {
SqlConnection conSender = sender as SqlConnection;
System.Diagnostics.Debug.WriteLine("连接状态改变:" + e.OriginalState.ToString() + " => " + e.CurrentState.ToString());
}
}
}