1.三層之間的關系:
三層是指:界面顯示層(UI),業務邏輯層(Business),數據操作層(Data Access)
文字描述:
ClIEnts對UI進行操作,UI調用Business進行相應的運算和處理,Business通過Data Access對Data Base進行操作。
1、增加了代碼的重用。Data Access可在多個項目中公用;Business可在同一項目的不同地方使用(如某個軟件B/S和C/S部分可以共用一系列的Business組件)。
2、使得軟件的分層更加明晰,便於開發和維護。美工人員可以很方便地設計UI設計,並在其中調用Business給出的接口,而程序開發人員則可以專注的進行代碼的編寫和功能的實現。
DataAgent類型中變量和方法的說明:
private string m_strConnectionString; //連接字符串
private OleDbConnection m_objConnection; //數據庫連接
public DataAgent(string strConnection) //構造方法,傳入的參數為連接字符串
private void OpenDataBase() //打開數據庫連接
private void #region CloseDataBase() //關閉數據庫連接
public DataView GetDataVIEw(string strSqlStat) //根據傳入的連接字符串返回DataVIEw
具體實現代碼如下:
public class DataAgent
{
#region Variables
private OleDbConnection m_objConnection;
#endregion Variables
#region Functions
#region DataAgend
/// <summary>
/// Initial Function
/// </summary>
/// <param name="strConnection"></param>
public DataAgent(string strConnection)
{
this.m_strConnectionString = strConnection;
}
#endregion DataAgend
#region OpenDataBase
/// <summary>
/// function to open data base
/// </summary>
private void OpenDataBase()
> {
try
{
this.m_objConnection = new OleDbConnection();
this.m_objConnection.ConnectionString = this.m_strConnectionString;
if(this.m_objConnection.State != ConnectionState.Open)
{
this.m_objConnection.Open();
}
}
{
throw e;
}
}
#endregion OpenDataBase
#region CloseDataBase
/// <summary>
/// the function to cloase data base
/// </summary>
private void CloseDataBase()
if(this.m_objConnection != null)
{
if(this.m_objConnection.State == ConnectionState.Open)
&n, bsp; {
this.m_objConnection.Close();
}
}
}
#endregion
#region GetDataVIEw
/// <summary>
/// Execute the sql and return the default table vIEw
/// </summary>
/// <param name="strSelectString">Select String</param>
/// <returns>DataVIEw of the DataTable</returns>
public DataView GetDataVIEw(string strSqlStat)
{
try
{
this.OpenDataBase();
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSqlStat.Trim(),this.m_objConnection);
objDataAdapter.Fill(objDataSet);
return objDataSet.Tables[0].DefaultVIEw;
}
catch(Exception e)
{
throw e;
}
finally
{
this.CloseDataBase();
}
}
#endregion GetDataTable
#endregion Functions
}