namespace WIS.Base.Data
{
/// <summary>
/// <table style="font-size:12px">
/// <tr><td><b>文 件 名</b>:DbObject.cs</td></tr>
/// <tr><td><b>功能描述</b>:數據層基類,提供對底層數據的基本操作</td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 [email protected] QQ:23106676</td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table>
/// </summary>
public class DbObject
{
#region 成員變量
/// <summary>
/// <table style="font-size:12px">
/// <tr><td><b>功能描述</b>:Oracle數據連接對象</td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table>
/// </summary>
protected OracleConnection Connection;
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:數據連接字符串</td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
private string connectionString;
#endregion
#region 構造函數
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:構造函數,使用配置文件中的默認數據連接字符串ConnectionString,初始化數據連接對象 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
public DbObject()
{
connectionString = ConfigurationSettings.APPSettings.Get("ConnectionString");//從Web.Config中取得的連接字符串
Connection = new OracleConnection(connectionString);
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:構造函數,根據指定的數據連接字符串,初始化數據連接對象</td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="newConnectionString">數據連接字符串</param>
public DbObject( string newConnectionString )
{
connectionString = newConnectionString;
Connection = new OracleConnection( connectionString );
}
#endregion
#region 私有方法
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:創建一個OracleCommand對象,用於生成OracleDataReader </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedProcName">存儲過程名稱</param>
/// <param name="parameters">存儲過程的參數對象列表(數組)</param>
/// <returns>OracleCommand對象</returns>
private OracleCommand BuildCommand(string storedProcName, IDataParameter[] parameters)
{
OracleCommand command = new OracleCommand( storedProcName, Connection );
command.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add( parameter );
}
return command;
}
#endregion
#region 運行存儲過程
/// <summary>
/// <table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行存儲過程,獲取影響行數,返回存儲過程運行結果 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table>
/// </summary>
/// <param name="storedProcName">存儲過程名稱</param>
/// <param name="parameters">存儲過程的參數對象列表(數組)</param>
/// <param name="rowsAffected">出參:執行存儲過程所影響的記錄行數</param>
/// <returns>存儲過程的運行結果</returns>
public object RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
{
object result;
//if(Connection.State.ToString() == "Closed") Connection.Open();
Connection.Open();
OracleCommand command = BuildCommand( storedProcName, parameters );
rowsAffected = command.ExecuteNonQuery();
//如果有"ReturnValue"參數則返回值,否則返回null
bool blnHasReturn = false;
for (int i=0;i<parameters.Length;i++)
{
if (parameters[i].Direction == ParameterDirection.ReturnValue)
{
blnHasReturn = true;
break;
}
}
if (blnHasReturn)
result = command.Parameters["ReturnValue"].Value;
else
result = null;
Connection.Close();
return result;
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行存儲過程,返回產生的OracleDataReader對象 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedProcName">存儲過程名稱</param>
/// <param name="parameters">存儲過程的參數對象列表(數組)</param>
/// <returns>OracleDataReader對象</returns>
public OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
{
OracleDataReader returnReader;
Connection.Open();
OracleCommand command = BuildCommand( storedProcName, parameters );
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader();
//connection.Close();
return returnReader;
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行存儲過程,創建一個DataSet對象,
/// 將運行結果存入指定的DataTable中,返回DataSet對象 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedProcName">存儲過程名稱</param>
/// <param name="parameters">存儲過程的參數對象列表(數組)</param>
/// <param name="tableName">數據表名稱</param>
/// <returns>DataSet對象</returns>
public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
{
DataSet dataSet = new DataSet();
Connection.Open();
OracleDataAdapter sqlDA = new OracleDataAdapter();
sqlDA.SelectCommand = BuildCommand( storedProcName, parameters );
sqlDA.Fill( dataSet, tableName );
Connection.Close();
return dataSet;
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行存儲過程,將運行結果存入已有DataSet對象的指定表中,無返回值 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedProcName">存儲過程名稱</param>
/// <param name="parameters">存儲過程的參數對象列表(數組)</param>
/// <param name="dataSet">DataSet對象</param>
/// <param name="tableName">數據表名稱</param>
public void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName )
{
Connection.Open();
OracleDataAdapter sqlDA = new OracleDataAdapter();
sqlDA.SelectCommand = BuildCommand( storedProcName, parameters );
sqlDA.Fill( dataSet, tableName );
Connection.Close();
}
#endregion
#region 運行SQL語句
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行與寫數據庫相關的SQL語句,返回影響行數 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlString">SQL語句</param>
/// <returns>影響行數</returns>
public int ExecNonQuery(string sqlString)
{
int RowAffected;
//if(Connection.State.ToString() == "Closed") Connection.Open();
Connection.Open();
OracleCommand command = new OracleCommand( sqlString, Connection );
RowAffected = command.ExecuteNonQuery();
Connection.Close();
return RowAffected;
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行SQL語句,返回OracleDataReader對象 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlString">SQL語句</param>
/// <returns>SqlDataReader對象</returns>
public OracleDataReader ExecSqlString(string sqlString)
{
OracleDataReader returnReader;
//if(Connection.State.ToString() == "Closed") Connection.Open();
Connection.Open();
OracleCommand command = new OracleCommand( sqlString, Connection );
returnReader = command.ExecuteReader();
//connection.Close();
return returnReader;
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行SQL語句,返回DataSet對象 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="string">SQL語句</param>
/// <param name="tableName">數據表名稱</param>
/// <returns>DataSet對象</returns>
public DataSet ExecSqlString(string sqlString, string tableName )
{
DataSet dataSet = new DataSet();
//if (Connection.State.ToString() == "Closed") Connection.Open();
Connection.Open();
OracleDataAdapter sqlDA = new OracleDataAdapter();
sqlDA.SelectCommand = new OracleCommand( sqlString, Connection );
sqlDA.Fill( dataSet, tableName );
Connection.Close();
return dataSet;
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行SQL語句,將運行結果存入已有DataSet對象的指定表中,無返回值 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlString">SQL語句</param>
/// <param name="dataSet">DataSet對象</param>
/// <param name="tableName">數據表名稱</param>
public void ExecSqlString(string sqlString, DataSet dataSet, string tableName )
{
//if (Connection.State.ToString() == "Closed") Connection.Open();
Connection.Open();
OracleDataAdapter sqlDA = new OracleDataAdapter();
sqlDA.SelectCommand = new OracleCommand( sqlString, Connection );
sqlDA.Fill( dataSet, tableName );
Connection.Close();
}
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:運行SQL語句,返回查詢結果的第一行的第一列,忽略其它行或列 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlString">SQL語句</param>
/// <returns>影響行數</returns>
public object ExecScalar(string sqlString)
{
object returnScalar;
//if (Connection.State.ToString() == "Closed") Connection.Open();
Connection.Open();
OracleCommand command = new OracleCommand( sqlString, Connection );
returnScalar = command.ExecuteScalar();
//Connection.Close();
return returnScalar;
}
#endregion
#region 關閉數據連接
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:關閉數據連接 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
public void Close()
{
if(Connection.State.ToString() == "Open")
Connection.Close();
}
#endregion
#region 析構函數
/// <summary><table style="font-size:12px">
/// <tr><td><b>功能描述</b>:析構函數,善後處理,釋放數據連接 </td></tr>
/// <tr><td><b>創 建 人</b>:夏春濤 </td></tr>
/// <tr><td><b>創建時間</b>:2005-05-28 </td></tr>
/// </table></summary>
~DbObject()
{
if(Connection.State.ToString() == "Open")
Connection.Close();
Connection.Dispose();
}
#endregion
}
}