为ASP.NET封装的SQL数据库访问类
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace MyCorporation.DepartMent.DataBase
{
/// <summary>
/// 通用数据库类
/// </summary>
public class DataBase
{
private string ConnStr = null;
public DataBase()
{
ConnStr = ConfigurationSettings.AppSettings["ConnStr"];
}
public DataBase(string Str)
{
try
{
this.ConnStr = Str;
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回connection对象
/// </summary>
/// <returns></returns>
public SqlConnection ReturnConn()
{
SqlConnection Conn = new SqlConnection(ConnStr);
Conn.Open();
return Conn;
}
public void Dispose(SqlConnection Conn)
{
if(Conn!=null)
{
Conn.Close();
Conn.Dispose();
}
GC.Collect();
}
/// <summary>
/// 运行SQL语句
/// </summary>
/// <param name="SQL"></param>
public void RunProc(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd ;
Cmd = CreateCmd(SQL, Conn);
try
{
Cmd.ExecuteNonQuery();
}
catch
{
throw new Exception(SQL);
}
Dispose(Conn);
return;
}
/// <summary>
/// 运行SQL语句返回DataReader
/// </summary>
/// <param name="SQL"></param>
/// <returns>SqlDataReader对象.</returns>
public SqlDataReader RunProcGetReader(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd ;
Cmd = CreateCmd(SQL, Conn);
SqlDataReader Dr;
try
{
Dr = Cmd.ExecuteReader(CommandBehavior.Default);
}
catch
{
throw new Exception(SQL);
}
//Dispose(Conn);
return Dr;
}
/// <summary>
/// 生成Command对象
/// </summary>
/// <param name="SQL"></param>