using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
namespace SQLHelper
{
///
/// SQLHelper 类封装对 SQL Server 数据库的添加、删除、修改和选择等操作
///
public class SQLHelper
{
/// 连接数据源
private SqlConnection myConnection = null;
private readonly string RETURNVALUE = "RETURNVALUE";
///
/// 打开数据库连接.
///
private void Open()
{
// 打开数据库连接
if (myConnection == null)
{
myConnection = new SqlConnection(ConfigurationManager.AppSetting
s["SQLCONNECTIONSTRING"].ToString());
}
if(myConnection.State == ConnectionState.Closed)
{
try
{
}
///打开数据库连接
myConnection.Open();
catch(Exception ex)
{
}
SystemError.CreateErrorLog(ex.Message);
finally
{
///关闭已经打开的数据库连接
/// 释放资源
///
public void Dispose()
{
}
// 确认连接是否已经关闭
if (myConnection != null)
{
}
myConnection.Dispose();
myConnection = null;
///
/// 执行存储过程
///
///
存储过程的名称
///
返回存储过程返回值
public int RunProc(string procName)
{
SqlCommand cmd = CreateProcCommand(procName, null);
try
{
}
///执行存储过程
cmd.ExecuteNonQuery();
catch(Exception ex)
{
}
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
finally
{
}
///关闭数据库的连接
Close();
///返回存储过程的参数值
return (int)cmd.Parameters[RETURNVALUE].Value;
}
///
/// 执行存储过程
///
///
存储过程名称
///
存储过程所需参数
///
返回存储过程返回值
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateProcCommand(procName, prams);
try
{
}
///执行存储过程
cmd.ExecuteNonQuery();
catch(Exception ex)
{
}
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
finally
{
///关闭数据库的连接
Close();
}
///返回存储过程的参数值
return (int)cmd.Parameters[RETURNVALUE].Value;
}
///
/// 执行存储过程
///
///
存储过程的名称
///
返回存储过程返回值
public void RunProc(string procName, out SqlDataReader dataReader)
{
///创建 Command
SqlCommand cmd = CreateProcCommand(procName, null);
try
{
///读取数据
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnectio
n);
}
catch(Exception ex)
dataReader = null;
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
{
}
}
///
/// 执行存储过程
///
///
存储过程的名称
///
存储过程所需参数
///
返回 DataReader 对象
public void RunProc(string procName, SqlParameter[] prams, out SqlDataRe
ader dataReader)
{
///创建 Command
SqlCommand cmd = CreateProcCommand(procName, prams);