DBHelper类参考

 

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());
  }
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值