怎样用VS进行三层结构应用程序的开发Word文件下载.docx
《怎样用VS进行三层结构应用程序的开发Word文件下载.docx》由会员分享,可在线阅读,更多相关《怎样用VS进行三层结构应用程序的开发Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
![怎样用VS进行三层结构应用程序的开发Word文件下载.docx](https://file1.bdocx.com/fileroot1/2023-1/31/33c68938-24e0-43aa-8408-2e7c36dc741f/33c68938-24e0-43aa-8408-2e7c36dc741f1.gif)
privateOleDbConnectionm_objConnection;
//数据库连接
publicDataAgent(stringstrConnection)//构造方法,传入的参数为连接字符串
privatevoidOpenDataBase()//打开数据库连接
privatevoid#regionCloseDataBase()//关闭数据库连接
publicDataViewGetDataView(stringstrSqlStat)//根据传入的连接字符串返回DataView
具体实现代码如下:
publicclassDataAgent
{
#regionVariables
#endregionVariables
#regionFunctions
#regionDataAgend
///<
summary>
///InitialFunction
/summary>
paramname="
strConnection"
>
<
/param>
publicDataAgent(stringstrConnection)
this.m_strConnectionString=strConnection;
}
#endregionDataAgend
#regionOpenDataBase
///functiontoopendatabase
privatevoidOpenDataBase()
try
this.m_objConnection=newOleDbConnection();
this.m_objConnection.ConnectionString=this.m_strConnectionString;
if(this.m_objConnection.State!
=ConnectionState.Open)
this.m_objConnection.Open();
catch(Exceptione)
throwe;
#endregionOpenDataBase
#regionCloseDataBase
///thefunctiontocloasedatabase
privatevoidCloseDataBase()
if(this.m_objConnection!
=null)
if(this.m_objConnection.State==ConnectionState.Open)
this.m_objConnection.Close();
#endregion
#regionGetDataView
///Executethesqlandreturnthedefaulttableview
strSelectString"
SelectString<
returns>
DataViewoftheDataTable<
/returns>
publicDataViewGetDataView(stringstrSqlStat)
this.OpenDataBase();
OleDbDataAdapterobjDataAdapter=newOleDbDataAdapter(strSqlStat.Trim(),this.m_objConnection);
DataSetobjDataSet=newDataSet();
objDataAdapter.Fill(objDataSet);
returnobjDataSet.Tables[0].DefaultView;
finally
this.CloseDataBase();
#endregionGetDataTable
#endregionFunctions
3.Business的具体实现:
建立名为Base的类,此类作为其他事务类的基类,其中定义了一个DataAgent的实例。
其他所有的Business类都从该改类派生。
在该类中添加对DataAgent的引用,使所有的事务类都能使用DataAgent中的方法。
Base.cs源代码:
publicabstractclassBase
#regionDataAgent
privateDataAgentm_objDBAgent;
protectedDataAgentOleDBAgent
{
get
{
if(this.m_objDBAgent==null)
{
this.m_objDBAgent=this.CreateAgent();
}
returnthis.m_objDBAgent;
}
set
this.m_objDBAgent=value;
}
#endregionDataAgent
publicBase()
#regionCreateAgent
///<
///CreateanewDataAgent
theDataAgent<
privateDataAgentCreateAgent()
stringstrConnection=ConfigurationSettings.AppSettings["
ConnectionString"
];
returnnewDataAgent(strConnection);
#endregionCreateAgent
准备好了数据操作层和事务层的基类,底下就可以正式地开始业务逻辑类的开发了,如有一个显示新闻的类News,其中包含了一个GetNewsLsit()的方法,该方法用来获取所有的新闻标题列表,代码如下:
publicclassNews:
Base
publicNewsContact()
publicDataViewGetNewsList()
stringstrSql;
strSql="
"
;
strSql+="
SELECTTop10NewsId,NewsTitle"
FROMTb_News"
WHERENewsEnable=1"
ORDERBYNewsId"
returnbase.OleDBAgent.GetDataView(strSql);
由于数据库结构比较简单,在此就不再给出详细的表结构。
4.UI层对Business中接口的调用
首先,在页面中添加对News类的引用。
然后,在页面中添加一个(DataGrid)dgNews用来显示新闻列表。
在PageBehind的Page_Load方法中添加如下代码:
NewsobjNews=newNews();
this.dgNews.DataSource=objNews.GetNewsList();
this.dgNews.DataBind();
至此,大功告成!
publicclassDataAgent
/////////////////////////////////////
用VS2005.NET进行三层结构应用程序的开发
privatestringm_strConnectionString;
privateOleDbConnectionm_objConnection;
///<
///InitialFunction
publicDataAgent(stringstrConnection)
this.m_strConnectionString=strConnection;
///functiontoopendatabase
privatevoidOpenDataBase()
try
this.m_objConnection=newOleDbConnection();
this.m_objConnection.ConnectionString=this.m_strConnectionString;
if(this.m_objConnection.State!
this.m_objConnection.Open();
catch(Exceptione)
throwe;
///thefunctiontocloasedatabase
privatevoidCloseDataBase()
if(this.m_objConnection!
if(this.m_objConnection.State==ConnectionState.Open)
this.m_objConnection.Close();
///Executethesqlandreturnthedefaulttableview
publicDataViewGetDataView(stringstrSqlStat)
this.OpenDataBase();
returnobjDataSet.Tables[0].DefaultView;
finally
this.CloseDataBase();
#regionDataAgent
privateDataAgentm_objDBAgent;
protectedDataAgentOleDBAgent
get
if(this.m_objDBAgent==null)
this.m_objDBAgent=this.CreateAgent();
returnthis.m_objDBAgent;
set
this.m_objDBAgent=value;
#endregionDataAgent
publicBase()
#regionCreateAgent
///CreateanewDataAgent
privateDataAgentCreateAgent()
stringstrConnection=ConfigurationSettings.AppSettings["
returnnewDataAgent(strConnection);
#endregionCreateAgent
准备好了数据操作层和事务层的基类,底下就可以正式地开始业务逻辑类的开发