SQLHelper.cs 文件
2007-10-10 14:35
435 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SQLHelper.cs 文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//=================================================================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// This file is based on the Microsoft Data Access Application Block for .NET
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// For more information please go to http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//=================================================================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data.SqlClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
namespace PetShop.DBUtility ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// The SqlHelper class is intended to encapsulate high performance,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// scalable best practices for common uses of SqlClient.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public abstract class SqlHelper ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Database connection strings
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings[“SQLConnString1“].ConnectionString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings[“SQLConnString2“].ConnectionString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings[“SQLConnString3“].ConnectionString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings[“SQLProfileConnString“].ConnectionString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Hashtable to store cached parameters
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// using the provided parameters.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// e.g.:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, “PublishOrders“, new SqlParameter(“@prodid“, 24));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“connectionString“〉a valid connection string for a SqlConnection〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandType“〉the CommandType (stored procedure, text, etc.)〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandText“〉the stored procedure name or T-SQL command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandParameters“〉an array of SqlParamters used to execute the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉an int representing the number of rows affected by the command〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand cmd = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
using (SqlConnection conn = new SqlConnection(connectionString)) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int val = cmd.ExecuteNonQuery();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return val;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Execute a SqlCommand (that returns no resultset) against an existing database connection
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// using the provided parameters.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// e.g.:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, “PublishOrders“, new SqlParameter(“@prodid“, 24));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“conn“〉an existing database connection〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandType“〉the CommandType (stored procedure, text, etc.)〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandText“〉the stored procedure name or T-SQL command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandParameters“〉an array of SqlParamters used to execute the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉an int representing the number of rows affected by the command〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand cmd = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int val = cmd.ExecuteNonQuery();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return val;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// using the provided parameters.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// e.g.:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, “PublishOrders“, new SqlParameter(“@prodid“, 24));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“trans“〉an existing sql transaction〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandType“〉the CommandType (stored procedure, text, etc.)〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandText“〉the stored procedure name or T-SQL command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandParameters“〉an array of SqlParamters used to execute the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉an int representing the number of rows affected by the command〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand cmd = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int val = cmd.ExecuteNonQuery();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return val;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Execute a SqlCommand that returns a resultset against the database specified in the connection string
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// using the provided parameters.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// e.g.:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, “PublishOrders“, new SqlParameter(“@prodid“, 24));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“connectionString“〉a valid connection string for a SqlConnection〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandType“〉the CommandType (stored procedure, text, etc.)〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandText“〉the stored procedure name or T-SQL command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandParameters“〉an array of SqlParamters used to execute the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉A SqlDataReader containing the results〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand cmd = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection(connectionString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// we use a try/catch here because if the method throws an exception we want to
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// close the connection throw code, because no datareader will exist, hence the
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// commandBehaviour.CloseConnection will not work
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return rdr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// using the provided parameters.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// e.g.:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, “PublishOrders“, new SqlParameter(“@prodid“, 24));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“connectionString“〉a valid connection string for a SqlConnection〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandType“〉the CommandType (stored procedure, text, etc.)〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandText“〉the stored procedure name or T-SQL command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandParameters“〉an array of SqlParamters used to execute the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉An object that should be converted to the expected type using Convert.To{Type}〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand cmd = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
using (SqlConnection connection = new SqlConnection(connectionString)) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object val = cmd.ExecuteScalar();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return val;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Execute a SqlCommand that returns the first column of the first record against an existing database connection
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// using the provided parameters.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// e.g.:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, “PublishOrders“, new SqlParameter(“@prodid“, 24));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/remarks〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“conn“〉an existing database connection〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandType“〉the CommandType (stored procedure, text, etc.)〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandText“〉the stored procedure name or T-SQL command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“commandParameters“〉an array of SqlParamters used to execute the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉An object that should be converted to the expected type using Convert.To{Type}〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand cmd = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object val = cmd.ExecuteScalar();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return val;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// add parameter array to the cache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“cacheKey“〉Key to the parameter cache〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈param name=“cmdParms“〉an array of SqlParamters to be cached〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parmCache[cacheKey] = commandParameters;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Retrieve cached parameters
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“cacheKey“〉key used to lookup parameters〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈returns〉Cached SqlParamters array〈/returns〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static SqlParameter[] GetCachedParameters(string cacheKey) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (cachedParms == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0, j = cachedParms.Length; i 〈 j; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return clonedParms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//**//**//// 〈summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Prepare a command for execution
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈/summary〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“cmd“〉SqlCommand object〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“conn“〉SqlConnection object〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“trans“〉SqlTransaction object〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“cmdType“〉Cmd type e.g. stored procedure or text〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 〈param name=“cmdText“〉Command text, e.g. Select * from Products〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// 〈param name=“cmdParms“〉SqlParameters to use in the command〈/param〉
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (conn.State != ConnectionState.Open)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Connection = conn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.CommandText = cmdText;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (trans != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Transaction = trans;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.CommandType = cmdType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (cmdParms != null) ......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (SqlParameter parm in cmdParms)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cmd.Parameters.Add(parm);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- sqlHelper.cs文件样板
- SQLHelper.cs 文件
- SQLHelper.cs文件代码
- SQLHelper.cs 文件
- sqlHelper.cs文件样板
- SQLHelper.cs 文件
- 防止连接泄漏的两种SQLHelper.cs文件写法
- 防止连接泄漏的两种SQLHelper.cs文件写法
- 用于sqlite数据库的SqlLiteHelper.cs 的类
- C# SQLHelper.cs
- 利用SqlHelper.cs实现Web程序与数据库的连接
- SqlHelper.cs 使用时要注意的地方
- Sqlhelper.cs--数据库操作类
- SqlHelper.cs
- SqlHelper.cs
- SQLHelper+配置文件+参数查询
- 做项目时写的数据库操作类。SqlHelper.cs(三)
- SQLHelper.cs
- sqlhelper.cs
- SqlHelper.cs