// // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // .NET Service Configuration V2.0 for Design Considerations for Service-Oriented Applications based on Windows Communication Foundation. Created with Microsoft .NET Framework 3.5 and Microsoft Visual Studio. Copyright 2008, Microsoft Corporation. //====================================================================================================== //This file contains SQLServerHelper logic, largely derived/modified from the MSDN Data Access Block. It //has the methods that perform ALL ADO.NET operations. //====================================================================================================== using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Collections; namespace Trade.DALSQLServer { public abstract class SQLServerHelper { // Hashtable to store cached parameters private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// /// Create and execute a command to return DataReader after binding to a single parameter. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// The single SqlParameter object to bind to the query. public static SqlDataReader ExecuteReaderSingleParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter singleParm) { SqlCommand cmd = new SqlCommand(); if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; cmd.Parameters.Add(singleParm); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult); return rdr; } /// /// Create and execute a command to return a single-row DataReader after binding to a single parameter. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// The single SqlParameter object to bind to the query. public static SqlDataReader ExecuteReaderSingleRowSingleParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter singleParm) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; cmd.Parameters.Add(singleParm); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleRow); return rdr; } /// /// Create and execute a command to return a single-row DataReader after binding to multiple parameters. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// An array of SqlParameter objects to bind to the query. public static SqlDataReader ExecuteReaderSingleRow(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; PrepareCommand(cmd, cmdParms); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleRow); return rdr; } /// /// Create and execute a command to return a DataReader, no parameters used in the command. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type public static SqlDataReader ExecuteReaderNoParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult); return rdr; } /// /// Create and execute a command to return a DataReader after binding to multiple parameters. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// An array of SqlParameter objects to bind to the query. public static SqlDataReader ExecuteReader(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; PrepareCommand(cmd, cmdParms); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult); return rdr; } /// /// Create and execute a command to return a single scalar (int) value after binding to multiple parameters. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// An array of SqlParameter objects to bind to the query. public static int ExecuteScalar(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = cmdText; cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; PrepareCommand(cmd, cmdParms); int val = Convert.ToInt32(cmd.ExecuteScalar()); return val; } /// /// Create and execute a command to return a single scalar (int) value after binding to a single parameter. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// A SqlParameter object to bind to the query. public static int ExecuteScalarSingleParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter singleParm) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = cmdText; cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.Parameters.Add(singleParm); int val = Convert.ToInt32(cmd.ExecuteScalar()); return val; } /// /// Create and execute a command to return a single scalar (int) value. No parameters will be bound to the command. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// A SqlParameter object to bind to the query. public static object ExecuteScalarNoParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = cmdText; cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; object val = cmd.ExecuteScalar(); return val; } /// /// Create and execute a command that returns no result set after binding to multiple parameters. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// An array of SqlParameter objects to bind to the query. public static int ExecuteNonQuery(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; PrepareCommand(cmd, cmdParms); int val = cmd.ExecuteNonQuery(); return val; } /// /// Create and execute a command that returns no result set after binding to a single parameter. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// A SqlParameter object to bind to the query. public static int ExecuteNonQuerySingleParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter singleParam) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; cmd.Parameters.Add(singleParam); int val = cmd.ExecuteNonQuery(); return val; } /// /// Create and execute a command that returns no result set after binding to a single parameter. /// /// Connection to execute against. If not open, it will be here. /// ADO transaction. If null, will not be attached to the command /// Type of ADO command; such as Text or Procedure /// The actual SQL or the name of the Stored Procedure depending on command type /// A SqlParameter object to bind to the query. public static int ExecuteNonQueryNoParm(SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText) { if (conn.State != ConnectionState.Open) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (trans != null) cmd.Transaction = trans; cmd.CommandText = cmdText; int val = cmd.ExecuteNonQuery(); return val; } /// /// add parameter array to the cache /// /// Key to the parameter cache /// an array of SqlParamters to be cached public static void CacheParameters(string cacheKey, params SqlParameter[] cmdParms) { parmCache[cacheKey] = cmdParms; } /// /// Retrieve cached parameters /// /// key used to lookup parameters /// Cached SqlParamters array public static SqlParameter[] GetCacheParameters(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; } /// /// Prepare a command for execution /// /// SqlCommand object /// SqlConnection object /// SqlTransaction object /// Cmd type e.g. stored procedure or text /// Command text, e.g. Select * from Products /// SqlParameters to use in the command private static void PrepareCommand(SqlCommand cmd, SqlParameter[] cmdParms) { if (cmdParms != null) { for (int i = 0; i < cmdParms.Length; i++) { SqlParameter parm = (SqlParameter)cmdParms[i]; cmd.Parameters.Add(parm); } } } } }