我的database.cs 都在这了 不太懂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// 通用的数据库处理类,通过ADO.NET与数据库连接
/// </summary>
public class Database:IDisposable
{
private SqlConnection con;
public Database()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// 打开数据库连接
/// </summary>
private void Open()
{
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["CONNECTIONSTRING"].ToString());
}
if (con.State == ConnectionState.Closed)
{
try
{
con.Open();
}
catch (Exception ex)
{
SystemError.SystemLog(ex.Message);
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
/// <summary>
/// 执行存储过程
/// </summary>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
SystemError.SystemLog(ex.Message);
}
Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
SystemError.SystemLog(ex.Message);
}
int r = (int)cmd.Parameters["ReturnValue"].Value;
Close();
return r;
}
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 创建一个SqlCommand对象以此来执行存储过程
/// </summary>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
}
cmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
return cmd;
}
/// <summary>
/// 生成存储过程参数
/// </summary>
public SqlParameter CreateParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (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;
}
public SqlParameter CreateInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return CreateParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
public SqlParameter CreateOutParam(string ParamName, SqlDbType DbType, int Size)
{
return CreateParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
public SqlParameter CreateReturnParam(string ParamName, SqlDbType DbType, int Size)
{
return CreateParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null);
}
public SqlDataReader ExecuteSQL(string sql)
{
SqlDataReader dr = null;
Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
}