为ASP.NET封装的SQL数据库访问类

2008-02-22 09:29:54来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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>
/// <param name="Conn"></param>
/// <returns></returns>
public SqlCommand CreateCmd(string SQL, SqlConnection Conn)
{
SqlCommand Cmd ;
Cmd = new SqlCommand(SQL, Conn);
return Cmd;
}

/// <summary>
/// 生成Command对象
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public SqlCommand CreateCmd(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd ;
Cmd = new SqlCommand(SQL, Conn);
return Cmd;
}

/// <summary>
/// 返回adapter对象
/// </summary>
/// <param name="SQL"></param>
/// <param name="Conn"></param>
/// <returns></returns>
public SqlDataAdapter CreateDa(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
Da = new SqlDataAdapter(SQL, Conn);
return Da;
}

/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
public DataSet RunProc(string SQL ,DataSet Ds)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
//Da = CreateDa(SQL, Conn);
Da = new SqlDataAdapter(SQL,Conn);
try
{
Da.Fill(Ds);
}
catch(Exception Err)
{
throw Err;
}
Dispose(Conn);
return Ds;
}

/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
/// <param name="dataReader">表名</param>
public DataSet RunProc(string SQL ,DataSet Ds,string tablename)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
Da = CreateDa(SQL);
try
{
Da.Fill(Ds,tablename);
}
catch(Exception Ex)
{
throw Ex;
}
Dispose(Conn);
return Ds;
}

/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
/// <param name="dataReader">表名</param>
public DataSet RunProc(string SQL , DataSet Ds ,int StartIndex ,int PageSize, string tablename )
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da ;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET Web Page应用深入探讨

下一篇:asp向asp.net应用程序的转变过程