/*****************************************************
* 文 件 名:DBObject.cs
* 功能描述:定义数据层基类。
* 创 建 人:夏春涛 xchuntao@163.com qq:23106676
* 创建时间:2004-08-11 11:05
*****************************************************/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace XD.XLB.JBC.JBInfo.WebModules.Data
{
///
/// 数据层基类,提供对底层数据的基本操作
///
public class DBObject
{
private SqlConnection connection;
#region 构造函数
///
/// 构造函数,初始化数据连接对象
///
public DBObject()
{
string connectionString = ConfigurationSettings.AppSettings.Get("ConnectionString",',',');//从Web.Config中取得的连接字符串
connection = new SqlConnection(connectionString,',',');
}
///
/// 功能描述:构造函数,根据指定的数据连接字符串,初始化数据连接对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// 数据连接字符串
public DBObject( string newConnectionString )
{
string connectionString = newConnectionString;
connection = new SqlConnection( connectionString ,',',');
}
#endregion
///
/// 数据连接对象(只读)
///
public SqlConnection Connection
{
get
{
return connection;
}
set
{
connection = value;
}
}
//-----------------------------------------------------------------------------------------
//以下是从《ASP.Net Web站点高级编程》中Copy的(夏春涛)------------------------------------
//-----------------------------------------------------------------------------------------
///
/// 创建一个SqlCommand对象,用于获取存储过程的返回值
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
///
private SqlCommand BuildIntCommand(string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand( storedProcName, parameters ,',',');
command.Parameters.Add( new SqlParameter ( "ReturnValue",
SqlDbType.Int,
4, /* Size */
ParameterDirection.ReturnValue,
false, /* is nullable */
0, /* byte precision */
0, /* byte scale */
string.Empty,
DataRowVersion.Default,
null ),',',');
return command;
}
///
/// 创建一个SqlCommand对象,用于生成SqlDataReader
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
///
private SqlCommand BuildQueryCommand(string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand( storedProcName, connection ,',',');
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add( parameter ,',',');
}
return command;
}
///
/// 运行存储过程,获取影响数,返回存储过程运行结果
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
/// 出参:执行存储过程所影响的记录行数
///
public object RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
{
object result;
if(connection.State.ToString() == "Closed")
connection.Open(,',',');
SqlCommand command = BuildIntCommand( storedProcName, parameters ,',',');
rowsAffected = command.ExecuteNonQuery(,',',');
result = command.Parameters["ReturnValue"].Value;
connection.Close(,',',');
return result;
}
///
/// 运行存储过程,返回产生的SqlDataReader对象
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
///
public SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
{
SqlDataReader returnReader;
connection.Open(,',',');
SqlCommand command = BuildQueryCommand( storedProcName, parameters ,',',');
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader(,',',');
//connection.Close(,',',');
return returnReader;
}
///
/// 运行存储过程,创建一个DataSet对象,
/// 将运行结果存入指定的DataTable中,返回DataSet对象
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
/// 数据表名称
///
public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
{
DataSet dataSet = new DataSet(,',',');
connection.Open(,',',');
SqlDataAdapter sqlDA = new SqlDataAdapter(,',',');
sqlDA.SelectCommand = BuildQueryCommand( storedProcName, parameters ,',',');
sqlDA.Fill( dataSet, tableName ,',',');
connection.Close(,',',');
return dataSet;
}
///
/// 运行存储过程,将运行结果存入已有DataSet对象的指定表中,无返回值
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
/// DataSet对象
/// 数据表名称
public void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName )
{
connection.Open(,',',');
SqlDataAdapter sqlDA = new SqlDataAdapter(,',',');
sqlDA.SelectCommand = BuildIntCommand( storedProcName, parameters ,',',');
sqlDA.Fill( dataSet, tableName ,',',');
connection.Close(,',',');
}
//-----------------------------------------------------------------------------------------
//以下是自建的(夏春涛)-------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
///
/// 运行与写数据库相关的SQL语句,返回影响行数**********************************************
///
/// SQL语句
///
public int ExeNonQuery(string sqlString)
{
int RowAffected;
if(connection.State.ToString() == "Closed")
connection.Open(,',',');
SqlCommand command = new SqlCommand( sqlString, connection ,',',');
RowAffected = command.ExecuteNonQuery(,',',');
//connection.Close(,',',');
return RowAffected;
}
///
/// 运行SQL语句,返回SqlDataReader对象
///
/// SQL语句
///
public SqlDataReader ExeSqlString(string sqlString)
{
SqlDataReader returnReader;
if(connection.State.ToString() == "Closed")
connection.Open(,',',');
SqlCommand command = new SqlCommand( sqlString, connection ,',',');
returnReader = command.ExecuteReader(,',',');
//connection.Close(,',',');
return returnReader;
}
///
/// 运行SQL语句,返回DataSet对象
///
/// SQL语句
/// 数据表名称
///
public DataSet ExeSqlString(string sqlString, string tableName )
{
DataSet dataSet = new DataSet(,',',');
if (connection.State.ToString() == "Closed")
connection.Open(,',',');
SqlDataAdapter sqlDA = new SqlDataAdapter(,',',');
sqlDA.SelectCommand = new SqlCommand( sqlString, connection ,',',');
sqlDA.Fill( dataSet, tableName ,',',');
connection.Close(,',',');
return dataSet;
}
///
/// 运行SQL语句,将运行结果存入已有DataSet对象的指定表中,无返回值
///
/// SQL语句
/// DataSet对象
/// 数据表名称
public void ExeSqlString(string sqlString, DataSet dataSet, string tableName )
{
if (connection.State.ToString() == "Closed")
connection.Open(,',',');
SqlDataAdapter sqlDA = new SqlDataAdapter(,',',');
sqlDA.SelectCommand = new SqlCommand( sqlString, connection ,',',');
sqlDA.Fill( dataSet, tableName ,',',');
connection.Close(,',',');
}
///
/// 运行SQL语句,返回查询结果的第一行的第一列,忽略其它行或列
///
/// SQL语句
///
public object ExeScalar(string sqlString)
{
object returnScalar;
if (connection.State.ToString() == "Closed")
connection.Open(,',',');
SqlCommand command = new SqlCommand( sqlString, connection ,',',');
returnScalar = command.ExecuteScalar(,',',');
//connection.Close(,',',');
return returnScalar;
}
~DBObject()
{
if(connection.State.ToString() == "Open")
connection.Close(,',',');
connection.Dispose(,',',');
}
}
}