using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using AHUT_TetWebERP;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
SqlConnection conn = null;
SqlDataAdapter adapter = null;
SqlCommand command = null;
SqlDataReader reader = null;
DataSet ds = null;
public DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 数据库连接
/// </summary>
public void connection()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BWConnectionString"].ToString());
conn.Open();
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseConnection()
{
if (conn != null)
conn.Close();
}
/// <summary>
/// 返回数据记录集DataSet
/// <param name="sql">适配器的数据查询语句</param>
/// </summary>
public DataSet returnRecordset(string sql)
{
try
{
adapter = new SqlDataAdapter(sql, conn);
ds = new DataSet();
adapter.Fill(ds);
}
catch(Exception ex)
{
SystemError.SystemLog(ex.Message);
}
return ds;
}
/// <summary>
/// 判断是否存在满足SQL查询条件的记录
/// <param name="sql">数据查询语句</param>
/// </summary>
public bool ExtenRow(string sql)
{
command = new SqlCommand(sql, conn);
try
{
reader = command.ExecuteReader();
if (reader.Read())
{
reader.Close();
reader.Dispose();
return true;
}
else
{
reader.Close();
reader.Dispose();
return false;
}
}
catch
{
return false;
}
}
/// <summary>
/// 执行操作数据库的SQL语句
/// <param name="sql">数据操作语句</param>
/// </summary>
public void SqlOperate(string sql)
{
command = new SqlCommand(sql, conn);
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
SystemError.SystemLog(ex.Message);
}
}
/// <summary>
/// 仅执行command操作
/// </summary>
/// <param name="sql"></param>
public void SqlCommd(string sql)
{
command = new SqlCommand(sql, conn);
}
public void getReader(ref SqlDataReader reader, string sql)
{
command = new SqlCommand(sql, conn);
reader = command.ExecuteReader();
}
/// <summary>
/// 销毁SqlDateReader数据
/// </summary>
/// <param name="reader">需要销毁的SqlDateReader数据对象</param>
public void DisposeReader(ref SqlDataReader reader)
{
reader.Close();
reader.Dispose();
}
/// <summary>
/// 执行不带参数的存储过程返回一个整数
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <returns>返回存储过程的返回值</returns>
public int ExcuteProc(string procName)
{
command = CreateCommand(procName, null);
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
SystemError.SystemLog(ex.Message); //执行错误时写入日志
}
finally
{
conn.Close();
}
return (int)command.Parameters["ReturnValue"].Value; //返回存储过程的返回值
}
/// <summary>
/// 执行带参数的存储过程返回一个整数
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="procName">存储过程所需参数</param>
/// <returns>返回存储过程的返回值</returns>
public int ExcuteProc(string procName,SqlParameter[] prams)
{
command = CreateCommand(procName, prams);
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
SystemError.SystemLog(ex.Message); //执行错误时写入日志
}
finally
{
conn.Close();
}
return (int)command.Parameters["ReturnValue"].Value; //返回存储过程的返回值
}
/// <summary>
/// 执行不带参数的存储过程,可保存从数据库中获取的结果
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <returns>返回存储过程的返回值</returns>
public void ExcuteProc(string procName, out SqlDataReader reader)
{
command = CreateCommand(procName, null);
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 执行带参数的存储过程,可保存从数据库中获取的结果
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="procName">存储过程所需参数</param>
public void ExcuteProc(string procName, SqlParameter[] prams, out SqlDataReader reader)
{
command = CreateCommand(procName, prams);
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 创建一个SqlCommand对象以此来执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <return>返回SqlCommand对象</return>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
connection();
command = new SqlCommand(procName, conn);
command.CommandType = CommandType.StoredProcedure;
//-------------依次将参数传入存储过程
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
command.Parameters.Add(parameter);
}
}
//--------------------------------------
command.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
/// <summary>
/// 生成存储过程参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数方向</param>
/// <param name="Value">参数值</param>
/// <return>新的 parameter 对象</return>
public SqlParameter CreateParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0) //根据参数Size大小值判断是否使用Size参数,大于0时使用,否则不使用
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
param = new SqlParameter(ParamName, DbType);
}
param.Direction = Direction; //创建输出类型的参数
if (!(Direction == ParameterDirection.Output && Value == null))
{
param.Value = Value;
}
return param;
}
/// <summary>
/// 传入输入参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <return>新的 parameter 对象</return>
public SqlParameter CreateInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return CreateParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <return>新的 parameter 对象</return>
public SqlParameter CreateOutParam(string ParamName, SqlDbType DbType, int Size)
{
return CreateParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <return>新的 parameter 对象</return>
public SqlParameter CreateReturnParam(string ParamName, SqlDbType DbType, int Size)
{
return CreateParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null);
}
}