Miao Jack's Blog

天行健君子以自強不息 地勢坤君子以厚德載物

众里寻他千百度,蓦然回首,那人却在灯火阑珊处


好在找到了一个库作三层架构的;众里寻他千百度,蓦然回首,那人却在灯火阑珊处

要作就要作企业级的系统,要学就学好的范例;

微软的petshop的架构;

看来我要好好学习啊,等着天天向上啊!//===============================================================================
// This file is based on the Microsoft Data Access Application Block for .NET

//=============================================================================== 

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using PetShop.Utility;

namespace PetShop.SQLServerDAL {

 /// <summary>
 /// The SqlHelper class is intended to encapsulate high performance,
 /// scalable best practices for common uses of SqlClient.
 /// </summary>
 public abstract class SQLHelper {
  
  //Database connection strings
  public static readonly string CONN_STRING_NON_DTC = ConnectionInfo.DecryptDBConnectionString(ConfigurationSettings.AppSettings["SQLConnString1"]);
  public static readonly string CONN_STRING_DTC_INV = ConnectionInfo.DecryptDBConnectionString(ConfigurationSettings.AppSettings["SQLConnString2"]);  
  public static readonly string CONN_STRING_DTC_ORDERS = ConnectionInfo.DecryptDBConnectionString(ConfigurationSettings.AppSettings["SQLConnString3"]);
  
  // Hashtable to store cached parameters
  private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
 
  public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {

   SqlCommand cmd = new SqlCommand();

   using (SqlConnection conn = new SqlConnection(connString)) {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
    int val = cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    return val;
   }
  }

  public static int ExecuteNonQuery(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {

   SqlCommand cmd = new SqlCommand();

   PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
   int val = cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   return val;
  }

  public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {
   SqlCommand cmd = new SqlCommand();
   PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
   int val = cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   return val;
  }

  public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {
   SqlCommand cmd = new SqlCommand();
   SqlConnection conn = new SqlConnection(connString);

   // 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, cmdParms);
    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    cmd.Parameters.Clear();
    return rdr;
   }catch {
    conn.Close();
    throw;
   }
  }
  
  public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {
   SqlCommand cmd = new SqlCommand();

   using (SqlConnection conn = new SqlConnection(connString)) {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
    object val = cmd.ExecuteScalar();
    cmd.Parameters.Clear();
    return val;
   }
  }
  public static object ExecuteScalar(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {
   
   SqlCommand cmd = new SqlCommand();

   PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
   object val = cmd.ExecuteScalar();
   cmd.Parameters.Clear();
   return val;
  }

  public static void CacheParameters(string cacheKey, params SqlParameter[] cmdParms) {
   parmCache[cacheKey] = cmdParms;
  }
  public static SqlParameter[] GetCachedParameters(string cacheKey) {
   SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
   
   if (cachedParms == null)
    return null;
   
   SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];

   for (int i = 0, j = cachedParms.Length; i < j; i++)
    clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();

   return clonedParms;
  }

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

阅读更多
个人分类:
想对作者说点什么? 我来说一句

rainmeter极品皮肤(时间日期)

2014年01月18日 226KB 下载

考研核心词汇 新东方

2010年05月01日 482KB 下载

嵌入式学习资料补充1

2011年08月25日 4.7MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭