You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
352 lines
13 KiB
352 lines
13 KiB
// ===========================================================
|
|
// 作者: 支巧 | MSN:zhiqiao84@hotmail.com | QQ:278494391
|
|
// 日期: 2009/02/13(最后更新时间)。
|
|
// 作用: 实用工具类,用于简化存储过程的调用。
|
|
// 备注: .Net Framework 2.0,SQL Server 2005 调试通过。
|
|
// ===========================================================
|
|
|
|
namespace PEIS.Utils
|
|
{
|
|
using System;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Collections;
|
|
using System.Configuration;
|
|
using System.Diagnostics;
|
|
using PEIS.Utils;
|
|
|
|
/// <summary>
|
|
/// 实用工具类,用于简化存储过程的调用。
|
|
/// </summary>
|
|
|
|
public class StoredProcedure
|
|
{
|
|
// 数据库连接字符串。
|
|
private string _connectionString = MySecurity.SDecryptString(ConfigurationManager.ConnectionStrings["ConnString"].ToString());
|
|
// 存储过程名称。
|
|
private string _storedProcedureName;
|
|
// 存储过程输出参数值列表。
|
|
private Hashtable _outputValues;
|
|
// 存储过程返回值
|
|
private int _returnValue;
|
|
|
|
/// <summary>
|
|
/// 初始化 StoredProceduer 对象。
|
|
/// </summary>
|
|
/// <param name="connectionString">数据库连接字符串。</param>
|
|
/// <example>
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr);
|
|
/// </example>
|
|
public StoredProcedure()
|
|
{
|
|
_storedProcedureName = string.Empty;
|
|
_outputValues = new Hashtable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化 StoredProceduer 对象。
|
|
/// </summary>
|
|
/// <param name="connectionString">数据库连接字符串。</param>
|
|
/// <param name="storeProcedureName">存储过程名称。</param>
|
|
/// <example>
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// string procName = "usp_xxxxxx";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr, procName);
|
|
/// </example>
|
|
public StoredProcedure(string storeProcedureName)
|
|
{
|
|
_storedProcedureName = storeProcedureName;
|
|
_outputValues = new Hashtable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取或设置存储过程名称。
|
|
/// </summary>
|
|
public string StoreProcedureName
|
|
{
|
|
get => _storedProcedureName; set => _storedProcedureName = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取输出参数值列表。
|
|
/// </summary>
|
|
/// <example>
|
|
/// object ret = sp.OutputValues["@p1"]; // @p1 是存储过程中的输出参数名。
|
|
/// </example>
|
|
public Hashtable OutputValues => _outputValues;
|
|
/// <summary>
|
|
/// 获取存储过程返回值。
|
|
/// int ret = sp.ReturnValue;
|
|
/// </summary>
|
|
public int ReturnValue
|
|
{
|
|
get { return _returnValue; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行 Insert/Delete/Update 存储过程。
|
|
/// </summary>
|
|
/// <param name="paraValues">传递给存储过程的参数值列表。</param>
|
|
/// <returns>受影响的行数。</returns>
|
|
/// <example>
|
|
/// int id = 1;
|
|
/// string name = "abc";
|
|
/// string phone = "123";
|
|
///
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// string procName = "usp_Insert";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr, procName);
|
|
/// try{
|
|
/// // 参数个数及顺序需要与存储过程中的一致。
|
|
/// sp.ExecuteNonQuery(id, name, phone);
|
|
/// switch(sp.ReturnValue){
|
|
/// case 1: break;
|
|
/// // ......
|
|
/// }
|
|
///
|
|
/// }catch(SqlException ex){
|
|
/// }catch(Exception ex){
|
|
/// }
|
|
/// </example>
|
|
/// <remarks>使用函数返回值时注意主子表级联更新的影响,
|
|
/// 建议使用 ReturnValue 而不是函数返回值来判断存储过程是否成功执行。
|
|
/// </remarks>
|
|
public int ExecuteNonQuery(params object[] paraValues)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
SqlCommand sqlCommand = CreateSqlCommand(connection);
|
|
try
|
|
{
|
|
DeriveParameters(sqlCommand);
|
|
AssignParameterValues(sqlCommand, paraValues);
|
|
connection.Open();
|
|
int affectedRowsCount = sqlCommand.ExecuteNonQuery();
|
|
_returnValue = (int)sqlCommand.Parameters["@RETURN_VALUE"].Value;
|
|
setOutputValues(sqlCommand);
|
|
connection.Close();
|
|
return affectedRowsCount;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Debug.WriteLine(ex.Message);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行存储过程,返回 System.Data.DataTable。
|
|
/// </summary>
|
|
/// <param name="paraValues">传递给存储过程的参数值列表。</param>
|
|
/// <returns>包含查询结果的 System.Data.DataTable。</returns>
|
|
/// <example>
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// string procName = "usp_Select";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr, procName);
|
|
///
|
|
/// int id = 1;
|
|
/// DataTable dt;
|
|
/// try{
|
|
/// // 参数个数及顺序需要与存储过程中的一致。
|
|
/// dt = sp.ExecuteDataTable(id);
|
|
/// }catch(SqlException ex){
|
|
/// }catch(Exception ex){
|
|
/// }
|
|
/// </example>
|
|
public DataTable ExecuteDataTable(params object[] paraValues)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(this._connectionString))
|
|
{
|
|
SqlCommand sqlCommand = this.CreateSqlCommand(connection);
|
|
try
|
|
{
|
|
DeriveParameters(sqlCommand);
|
|
AssignParameterValues(sqlCommand, paraValues);
|
|
SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
|
|
DataTable dataTable = new DataTable();
|
|
adapter.Fill(dataTable);
|
|
connection.Close();
|
|
return dataTable;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行存储过程,填充指定的 System.Data.DataTable。
|
|
/// </summary>
|
|
/// <param name="dataTable">用于填充查询结果的 System.Data.DataTable。</param>
|
|
/// <param name="paraValues">传递给存储过程的参数值列表。</param>
|
|
/// <example>
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// string procName = "usp_Select";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr, procName);
|
|
///
|
|
/// int id = 1;
|
|
/// DataTable dt = new DataTable();
|
|
/// try{
|
|
/// // 参数个数及顺序需要与存储过程中的一致。
|
|
/// sp.ExecuteFillDataTable(dt, id);
|
|
/// }catch(SqlException ex){
|
|
/// }catch(Exception ex){
|
|
/// }
|
|
/// </example>
|
|
public void ExecuteFillDataTable(DataTable dataTable, params object[] paraValues)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
SqlCommand sqlCommand = CreateSqlCommand(connection);
|
|
try
|
|
{
|
|
DeriveParameters(sqlCommand);
|
|
AssignParameterValues(sqlCommand, paraValues);
|
|
SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
|
|
adapter.Fill(dataTable);
|
|
connection.Close();
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行存储过程返回 System.Data.SqlClient.SqlDataReader,
|
|
/// 在 System.Data.SqlClient.SqlDataReader 对象关闭时,数据库连接自动关闭。
|
|
/// </summary>
|
|
/// <param name="paraValues">传递给存储过程的参数值列表。</param>
|
|
/// <returns>包含查询结果的 System.Data.SqlClient.SqlDataReader 对象。</returns>
|
|
/// <example>
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// string procName = "usp_Select";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr, procName);
|
|
///
|
|
/// int id = 1;
|
|
/// SqlDataReader sdr;
|
|
/// try{
|
|
/// // 参数个数及顺序需要与存储过程中的一致。
|
|
/// sdr = p.ExecuteDataReader(id);
|
|
/// }catch(SqlException ex){
|
|
/// }catch(Exception ex){
|
|
/// }
|
|
/// </example>
|
|
public SqlDataReader ExecuteDataReader(params object[] paraValues)
|
|
{
|
|
SqlConnection connection = new SqlConnection(_connectionString);
|
|
SqlCommand sqlCommand = CreateSqlCommand(connection);
|
|
try
|
|
{
|
|
DeriveParameters(sqlCommand);
|
|
AssignParameterValues(sqlCommand, paraValues);
|
|
connection.Open();
|
|
return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
|
|
/// </summary>
|
|
/// <param name="paraValues">传递给存储过程的参数值列表。</param>
|
|
/// <returns>结果集中第一行的第一列或空引用(如果结果集为空)。</returns>
|
|
/// <example>
|
|
/// string conStr = "@Data Source=server;Initial Catalog=database;Integrated Security=true";
|
|
/// string procName = "usp_Select";
|
|
/// StoredProcedure sp = new StoredProcedure(conStr, procName);
|
|
///
|
|
/// int id = 1;
|
|
/// string name = "";
|
|
/// try{
|
|
/// // 参数个数及顺序需要与存储过程中的一致。
|
|
/// name = sp.ExecuteScalar(id);
|
|
/// }catch(SqlException ex){
|
|
/// }catch(Exception ex){
|
|
/// }
|
|
/// </example>
|
|
public object ExecuteScalar(params object[] paraValues)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
SqlCommand sqlCommand = CreateSqlCommand(connection);
|
|
try
|
|
{
|
|
DeriveParameters(sqlCommand);
|
|
AssignParameterValues(sqlCommand, paraValues);
|
|
connection.Open();
|
|
object retVale = sqlCommand.ExecuteScalar();
|
|
connection.Close();
|
|
return retVale;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从在 System.Data.SqlClient.SqlCommand 中指定的存储过程中检索参数信息并填充指定的
|
|
/// System.Data.SqlClient.SqlCommand 对象的 System.Data.SqlClient.SqlCommand.Parameters 集合。
|
|
/// </summary>
|
|
/// <param name="sqlCommand">将从其中导出参数信息的存储过程的 System.Data.SqlClient.SqlCommand 对象。</param>
|
|
internal void DeriveParameters(SqlCommand sqlCommand)
|
|
{
|
|
try
|
|
{
|
|
sqlCommand.Connection.Open();
|
|
SqlCommandBuilder.DeriveParameters(sqlCommand);
|
|
sqlCommand.Connection.Close();
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
// 用指定的参数值列表为存储过程参数赋值。
|
|
private void AssignParameterValues(SqlCommand sqlCommand, params object[] paraValues)
|
|
{
|
|
if (paraValues != null)
|
|
{
|
|
if ((sqlCommand.Parameters.Count - 1) != paraValues.Length)
|
|
{
|
|
throw new Exception("传递的参数与存储过程参数不匹配。\n Stored Procedure Name: " + _storedProcedureName);
|
|
}
|
|
for (int i = 0; i < paraValues.Length; i++)
|
|
{
|
|
sqlCommand.Parameters[i + 1].Value = (paraValues[i] == null) ? DBNull.Value : paraValues[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
// 创建用于执行存储过程的 SqlCommand。
|
|
private SqlCommand CreateSqlCommand(SqlConnection connection)
|
|
{
|
|
SqlCommand command = new SqlCommand(_storedProcedureName, connection);
|
|
command.CommandType = CommandType.StoredProcedure;
|
|
return command;
|
|
}
|
|
|
|
// 获取存储过程的输出参数值列表。
|
|
private void setOutputValues(SqlCommand sqlCommand)
|
|
{
|
|
this._outputValues.Clear();
|
|
foreach (SqlParameter para in sqlCommand.Parameters)
|
|
{
|
|
if (para.Direction == ParameterDirection.Output || para.Direction == ParameterDirection.InputOutput)
|
|
{
|
|
_outputValues.Add(para.ParameterName, para.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|