一個資料庫操作類的執行個體(一.Command類部分)
來源:互聯網
上載者:User
command|資料|資料庫 using System;
namespace com.joybase.DB
{
/// <summary>
/// 資料庫操作類,通過這一個類的對外介面,可以獲得以下特性:
/// 1.不必區分資料庫類型,而去考慮是使用System.Data.SqlClient實現,或者使用System.Data.OleDB來實現;
/// 2.可以將SQL語句進行模式化,比如輸入“select * from tablename where username=? order by ? desc",然後再用建模語句將該語句保持完整;
/// 3.資料庫連接實現Connection Pool(該特性是建立在ADO.NET的基礎上的)
/// 4.支援SQL語句的DataReader輸出、無輸出或者DataSet輸出;
/// 5.支援ADO.NET事務級處理;
/// 6.支援預存程序的輸入及輸出;
/// </summary>
public class Command
{
//最終的SQL語句;
private System.Text.StringBuilder m_SQL;
//中間過程的SQL語句數組
private string[] m_SQLArr;
//替代字串的數組;
private string[] m_ReplaceText;
//替代字串的個數;
private int m_Count;
//連線類型;
private System.Data.CommandType m_CommandType;
//在設定檔中用來設定資料庫連接字串的標籤;
private string m_ConnstringSetName;
/// <summary>
/// 構造方法
/// </summary>
/// <param name="p_sql">模式SQL語句</param>
public Command(string p_sql)
{
if(p_sql==null) throw new Exception("Error Value");
if(p_sql.Trim()=="") throw new Exception("Error Value");
this.m_SQL=new System.Text.StringBuilder();
m_SQLArr=p_sql.Split('?');
m_Count=m_SQLArr.Length;
m_ReplaceText=new string[m_Count-1];
m_ConnstringSetName="";
this.m_CommandType=System.Data.CommandType.Text;
}
/// <summary>
/// 在設定檔中用來設定資料庫連接字串的標籤,如果不設定,則將採用“DataBase.ConnectionString”作為連接字串使用
/// </summary>
public string ConnStringSetName
{
set
{
this.m_ConnstringSetName=value;
}
}
/// <summary>
/// 所執行命令的類型,即是預存程序還是普通SQL語句;
/// </summary>
public System.Data.CommandType CommandType
{
set
{
this.m_CommandType=value;
}
}
/// <summary>
/// 以指定的字串替代在p_loaction位置上的“?”;
/// </summary>
/// <param name="p_location">位置索引</param>
/// <param name="p_InString">欲替代的字串</param>
public void setString(int p_location,string p_InString)
{
if(p_InString==null) throw new Exception("Error Value");
if(p_InString.Trim()==null) throw new Exception("Error Value");
m_ReplaceText[p_location-1]="'"+p_InString.Trim()+"'";
}
/// <summary>
/// 以指定的整數類型,替代在p_location位置上的"?"
/// </summary>
/// <param name="p_location">位置索引</param>
/// <param name="p_InInt">欲替代的整數</param>
public void setInt(int p_location,int p_InInt)
{
m_ReplaceText[p_location-1]=p_InInt.ToString();
}
/// <summary>
/// 以指定的長整數類型,替代在p_location位置上的"?"
/// </summary>
/// <param name="p_location">位置索引</param>
/// <param name="p_InLong">欲替代的長整數</param>
public void setLong(int p_location,int p_InLong)
{
m_ReplaceText[p_location-1]=p_InLong.ToString();
}
/// <summary>
/// 以指定的時間,替代在p_location位置上的"?"
/// </summary>
/// <param name="p_location">位置索引</param>
/// <param name="p_InDateTime">欲替代的時間</param>
public void setDateTime(int p_location,System.DateTime p_InDateTime)
{
m_ReplaceText[p_location-1]=p_InDateTime.ToString();
}
/// <summary>
/// 以系統時間替代在p_location位置的"?"
/// </summary>
/// <param name="p_location">位置索引</param>
/// <param name="p_OnLyDate">如果為True,則僅替代到日期,如果為False,則替代到時間</param>
public void setDateTime(int p_location,bool p_OnlyDate)
{
if(p_OnlyDate)
m_ReplaceText[p_location-1]=System.DateTime.Now.Date.ToString();
else
m_ReplaceText[p_location-1]=System.DateTime.Now.ToString();
}
private string JoinSQLString()
{
for(int i=0;i<m_Count;i++)
{
if(i!=0) this.m_SQL.Append(m_ReplaceText[i-1]);
this.m_SQL.Append(m_SQLArr[i]);
}
return this.m_SQL.ToString();
}
/// <summary>
/// 執行命令並且返回System.Data.IDataReader結果的結果集
/// </summary>
/// <returns></returns>
public System.Data.IDataReader Result()
{
System.Data.IDataReader result=null;
try
{
System.Data.IDbCommand command=Provider.getConn(m_ConnstringSetName).CreateCommand();
command.CommandText=this.JoinSQLString();
command.CommandType=this.m_CommandType;
command.Connection.Close();
command.Connection.Open();
command.Prepare();
//if(Provider.getConn(m_ConnstringSetName).State==System.Data.ConnectionState.Closed)
//Provider.getConn(m_ConnstringSetName).Open();
result=command.ExecuteReader();
//command.Connection.Close();
//Provider.getConn().Close();
}
catch
{
throw new Exception("error at execute db command");
}
return result;
}
/// <summary>
/// 無結果執行命令
/// </summary>
public void ExecuteNoResult()
{
try
{
System.Data.IDbCommand command=Provider.getConn(m_ConnstringSetName).CreateCommand();
command.CommandText=this.JoinSQLString();
command.CommandType=this.m_CommandType;
command.Connection.Close();
command.Connection.Open();
//if(Provider.getConn(m_ConnstringSetName).State==System.Data.ConnectionState.Closed)
//Provider.getConn(m_ConnstringSetName).Open();
command.ExecuteNonQuery();
}
catch
{
throw new Exception("Error at execute DB Command");
}
}
}
}