三層是指
文字描述
Clients對UI進行操作
優點
二
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 string m_strConnectionString;
private OleDbConnection m_objConnection;
#endregion Variables
#region Functions
#region DataAgend
///
/// Initial Function
///
///
public DataAgent(string strConnection)
{
this
}
#endregion DataAgend
#region OpenDataBase
///
/// function to open data base
///
private void OpenDataBase()
{
try
{
this
this
if(this
{
this
}
}
catch(Exception e)
{
throw e;
}
}
#endregion OpenDataBase
#region CloseDataBase
///
/// the function to cloase data base
///
private void CloseDataBase()
{
if(this
{
if(this
{
this
}
}
}
#endregion
#region GetDataView
///
/// Execute the sql and return the default table view
///
///
Select String
///
public DataView GetDataView(string strSqlStat)
{
try
{
this
OleDbDataAdapter objDataAdapter =
new OleDbDataAdapter(strSqlStat
DataSet objDataSet = new DataSet();
objDataAdapter
return objDataSet
}
catch(Exception e)
{
throw e;
}
finally
{
this
}
}
#endregion GetDataTable
#endregion Functions
}
三
建立名為Base的類
在該類中添加對DataAgent的引用
Base
public abstract class Base
{
#region DataAgent
private DataAgent m_objDBAgent;
protected DataAgent OleDBAgent
{
get
{
if(this
{
this
}
return this
}
set
{
this
}
}
#endregion DataAgent
public Base()
{
}
#region CreateAgent
///
/// Create a new DataAgent
///
///
private DataAgent CreateAgent()
{
string strConnection =
ConfigurationSettings
return new DataAgent(strConnection);
}
#endregion CreateAgent
}
准備好了數據操作層和事務層的基類
public class News: Base
{
public News Contact()
{
}
public DataView GetNewsList()
{
string strSql;
strSql =
strSql +=
strSql +=
strSql +=
strSql +=
return base
}
}
由於數據庫結構比較簡單
四
首先
然後
在Page Behind的Page_Load方法中添加如下代碼
News objNews = new News();
this
this
至此
From:http://tw.wingwit.com/Article/program/net/201311/13752.html