程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 怎樣用VS2005進行三層結構應用程序的開發

怎樣用VS2005進行三層結構應用程序的開發

編輯:.NET實例教程

1.三層之間的關系:

三層是指:界面顯示層(UI),業務邏輯層(Business),數據操作層(Data Access)

文字描述:

ClIEntsUI進行操作,UI調用Business進行相應的運算和處理,Business通過Data AccessData Base進行操作。

 優點:

1、增加了代碼的重用。Data Access可在多個項目中公用;Business可在同一項目的不同地方使用(如某個軟件B/SC/S部分可以共用一系列的Business組件)。

2、使得軟件的分層更加明晰,便於開發和維護。美工人員可以很方便地設計UI設計,並在其中調用Business給出的接口,而程序開發人員則可以專注的進行代碼的編寫和功能的實現。


2.Data Access的具體實現:

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

         /// <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();

                   }

              }

         

;     catch(Exception e)

              {

                   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);

        DataSet objDataSet = new DataSet();

                  objDataAdapter.Fill(objDataSet);

                   return objDataSet.Tables[0].DefaultVIEw;

              }

              catch(Exception e)

              {

                   throw e;

              }

              finally

              {

                   this.CloseDataBase();

         }

         }

         #endregion GetDataTable

         #endregion Functions

 

     }


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved