using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;
namespace GeneralDatabaseLayer
{
public class GeneralDatabaseLayer
{
/// <summary>
/// ADO.NET数据库操作的简单封装
/// Author:kklldog
/// Create Date:2008-06-25
/// </summary>
///
string DBType;//数据库类型
string connectionString;//连接字符串
public GeneralDatabaseLayer()
{
this.GetDBType();
this.GetConnectionString();
}
/// <summary>
/// 从配置文件里读取数据库类型赋值给DBType,并返回DBType的值
/// </summary>
/// <returns>DBType的值</returns>
/// Create Date:2008-07-1
public string GetDBType()
{
DBType = ConfigurationManager.AppSettings["DBType"];
return DBType;
}
/// <summary>
/// 不从配置文件里获取数据库的类型,直接通过参数DBType赋值。
/// </summary>
/// <param name="DBType">数据库类型</param>
/// <returns>返回void</returns>
/// Create Date:2008-07-1
public void SetDBType(string DBType)
{
this.DBType = DBType;
}
/// <summary>
/// 从配置文件里读取连接字符串,赋值给connectionString并返回connectionString的值
/// </summary>
/// <returns>connectionString的值</returns>
/// Create Date:2008-07-1
public string GetConnectionString()
{
connectionString = ConfigurationManager.AppSettings["ConnectionString"];
return connectionString;
}
/// <summary>
/// 不从配置文件里读取连接字符串,直接通过参数赋值给connectionString
/// </summary>
/// <returns>void</returns>
/// Create Date:2008-07-1
public void SetConnectionString(string connectionString)
{
this.connectionString = connectionString;
}
/// <summary>
/// 跟据SQL语句返回相应的DataSet
/// </summary>
/// <param name="strSql">要执行的SQL查询语句</param>
/// <returns>返回得到的DataSet</returns>
/// Create Date:2008-06-25
public DataSet GetDataSet(string strSql)
{
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);
DbConnection DbConn = DbProvider.CreateConnection();
DbDataAdapter DbAdapter = DbProvider.CreateDataAdapter();
DbCommand DbCMD = DbConn.CreateCommand();
DbConn.ConnectionString = this.connectionString;
DbCMD.Connection = DbConn;
DbCMD.CommandText = strSql;
DbAdapter.SelectCommand = DbCMD;
DataSet dataset = new DataSet();
try
{
DbConn.Open();
DbAdapter.Fill(dataset);
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return dataset;
}
/// <summary>
///执行带参数的SQL查询语句
/// </summary>
/// <param name="strSql">要执行的SQL查询语句</param>
/// <param name="Parameters">参数集</param>
/// <returns>返回得到的DataSet</returns>
/// Create Date:2008-07-5
public DataSet GetDataSet(string strSql, DbParameter[] Parameters)
{
DataSet dataset = new DataSet();
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);
DbConnection DbConn = DbProvider.CreateConnection();
DbDataAdapter DbAdapter = DbProvider.CreateDataAdapter();
DbCommand DbCMD = DbConn.CreateCommand();
try
{
DbConn.ConnectionString = this.connectionString;
DbCMD.Connection = DbConn;
DbCMD.CommandText = strSql;
DbCMD.Parameters.Clear();
DbCMD.Parameters.AddRange(Parameters);
DbAdapter.SelectCommand = DbCMD;
DbConn.Open();
DbAdapter.Fill(dataset);
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return dataset;
}
/// <summary>
/// 执行SQL操作语句返回受影响的记录条数,根据类变量connectionString的值来创建连接字符串
/// </summary>
/// <param name="strSql">要执行的SQL操作语句</param>
/// <returns>返回受影响的记录条数</returns>
/// Create Date:2008-06-25
public int ExecuteNonQuery(string strSql)
{
int iCount = -1;
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂
DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
DbConn.ConnectionString = this.connectionString;
try
{
DbCommand cmd = DbProvider.CreateCommand();
cmd.CommandText = strSql;
cmd.Connection = DbConn;
DbConn.Open();
iCount = cmd.ExecuteNonQuery();
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return iCount;
}
/// <summary>
/// 执行带参数的SQL操作语句
/// </summary>
/// <param name="strSql">要执行的SQL操作语句</param>
/// <param name="Parameters">参数集</param>
/// <returns>返回受影响的记录条数</returns>
/// Create Date:2008-06-25
public int ExecuteNonQuery(string strSql, DbParameter[] Parameters)
{
int iCount = -1;
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂
DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
try
{
DbConn.ConnectionString = this.connectionString;
DbCommand cmd = DbProvider.CreateCommand();
cmd.CommandText = strSql;
cmd.Parameters.Clear();
cmd.Parameters.AddRange(Parameters);
cmd.Connection = DbConn;
DbConn.Open();
iCount = cmd.ExecuteNonQuery();
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return iCount;
}
/// <summary>
/// 执行不带参数的存储过程,并返回受影响的行数
/// </summary>
/// <param name="ProcedureName">存储过程名称</param>
/// <returns>受影响的行数</returns>
/// Create Date:2008-07-1
public int ExecuteProcedure(string ProcedureName)
{
int iCount = -1;
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂
DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
DbCommand cmd = DbProvider.CreateCommand();
try
{
DbConn.ConnectionString = this.connectionString;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ProcedureName;
cmd.Connection = DbConn;
DbConn.Open();
iCount = cmd.ExecuteNonQuery();
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return iCount;
}
/// <summary>
/// 执行带参数的存储过程,并返回受影响的行数
/// </summary>
/// <param name="ProcedureName">存储过程名称</param>
/// <param name="Patameters">存储过程的参数集</param>
/// <returns>受影响的记录条数</returns>
/// Create Date:2008-07-1
public int ExecuteProcedure(string ProcedureName, DbParameter[] Parameters)
{
int iCount = -1;
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType); //创建数据库工厂
DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
DbConn.ConnectionString = this.connectionString;
DbCommand cmd = DbProvider.CreateCommand();
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ProcedureName;
cmd.Parameters.Clear();
cmd.Parameters.AddRange(Parameters);
cmd.Connection = DbConn;
DbConn.Open();
iCount = cmd.ExecuteNonQuery();
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return iCount;
}
/// <summary>
/// 执行不带参数的存储过程,并返回DataSet
/// </summary>
/// <param name="ProcedureName">存储过程名称</param>
/// <returns>DataSet</returns>
/// Create Date:2008-07-5
public DataSet ExectueProcGetDS(string ProcedureName)
{
DataSet DS = new DataSet();
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂
DbConnection DbConn = DbProvider.CreateConnection();//创建数据库连接对象
DbConn.ConnectionString = this.connectionString;
DbCommand cmd = DbProvider.CreateCommand();
DbDataAdapter DbAdapater = DbProvider.CreateDataAdapter();
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ProcedureName;
cmd.Connection = DbConn;
DbAdapater.SelectCommand = cmd;
DbConn.Open();
DbAdapater.Fill(DS);
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return DS;
}
/// <summary>
/// 执行带参数的存储过程,并返回DataSet
/// </summary>
/// <param name="ProcedureName">存储过程名称</param>
/// <param name="Patameters">存储过程的参数集</param>
/// <returns>DataSet</returns>
/// Create Date:2008-07-5
public DataSet ExectueProcGetDS(string ProcedureName, DbParameter[] Parameters)
{
DataSet DS = new DataSet();
DbProviderFactory DbProvider = DbProviderFactories.GetFactory(this.DBType);//创建数据库工厂
DbConnection DbConn = DbProvider.CreateConnection(); //创建数据库连接对象
DbConn.ConnectionString = this.connectionString;
DbCommand cmd = DbProvider.CreateCommand();
DbDataAdapter DbAdapater = DbProvider.CreateDataAdapter();
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ProcedureName;
cmd.Parameters.Clear();
cmd.Parameters.AddRange(Parameters);
cmd.Connection = DbConn;
DbAdapater.SelectCommand = cmd;
DbConn.Open();
DbAdapater.Fill(DS);
}
catch (DbException Ex)
{
throw new Exception(Ex.Message);
}
finally
{
if (DbConn != null)
DbConn.Close();
}
return DS;
}
}
}