程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 在c#中實現3層架構(4)

在c#中實現3層架構(4)

編輯:關於C語言

數據訪問層

數據層包括處理MS Access數據庫的細節。所有這些細節都是透明的,不會影響到商業邏輯層。數據訪問層有個指向商業邏輯層的引用BOCustomer cus。為了應用方便並且支持其他數據庫。

using System;
using System.Data.OleDb;
using System.Data;

namespace _3tIErarchitecture

{

  /// <SUMMARY>
  /// Summary description for DACustomer.
  /// </SUMMARY>
  public class DACustomer
  {
    private OleDbConnection cnn;
    //change connection string as per the
    //folder you unzip the files
    private const string CnnStr =
     "Provider=Microsoft.Jet.OLEDB.4.0;Data " +
     "Source= D:\Rahman_Backup\Programming\" +
       "Csharp\3tIErarchitecture\customer.mdb;";

    //local variables
    private String strTable="";
    private String strFIElds="";
    private String strValues="";
    private String insertStr="";
    //this needs to be changed based on customer
    //table fIElds' Name of the database!
    private const String thisTable = "tblCustomer";
    private const String cus_ID = "CUS_ID";
    private const String cus_LName = "CUS_L_NAME";
    private const String cus_FName = "CUS_F_NAME";
    private const String cus_Tel = "CUS_TEL";
    private const String cus_Address = "CUS_ADDRESS";

    public DACustomer()
    {
    }
    public DACustomer(BOCustomer cus)
    {
      // A reference of the business object class
    }
    //standard dataset function that adds a new customer

    public void Add(BOCustomer cus)
    {

      String str = BuildAddString(cus);
      OpenCnn();

      //Open command option - cnn parameter is imporant
      OleDbCommand cmd = new OleDbCommand(str,cnn);

      //execute connection
      cmd.ExecuteNonQuery();
      // close connection
      CloseCnn();
    }
    //standard dataset function that updates
    //details of a customer based on ID
    public void Update(BOCustomer cus)
    {
      OpenCnn();
      String selectStr = "UPDATE " + thisTable +
        " set " + cus_LName + " = '" + cus.LName + "'" +
        ", " + cus_FName + " = '" + cus.FName + "'" +
        ", " + cus_Address + " = '" + cus.Address + "'" +
        ", " + cus_Tel + " = '" + cus.Tel + "'" +
        " where cus_ID = '" + cus.cusID + "'";

      OleDbCommand cmd = new OleDbCommand(selectStr,cnn);

      cmd.ExecuteNonQuery();
      CloseCnn();
    }
    //standard dataset function that finds and
    //return the detail of a customer in a dataset
    public DataSet Find(String argStr)
    {
      DataSet ds=null;

      try
      {
        OpenCnn();
        String selectStr = "select * from " + thisTable +
               " where cus_ID = '" + argStr + "'";
        OleDbDataAdapter da =
            new OleDbDataAdapter(selectStr,cnn);
        ds = new DataSet();
        da.Fill(ds,thisTable);
        CloseCnn();

      }
      catch(Exception e)
      {
        String Str = e.Message;
      }

      return ds;
    }

    private void OpenCnn()
    {
      // initialise connection
      String cnnStr = CnnStr;
      cnn = new OleDbConnection(cnnStr);
      // open connection
      cnn.Open();
    }

    private void CloseCnn()
    {
      // 5- step five
      cnn.Close();
    }
    // just a supporting function that builds
    // and return the insert string for dataset.
    private String BuildAddString(BOCustomer cus)
    {
      // these are the constants as
      // set in the top of this module.
      strTable="Insert into " + thisTable;
      strFIElds=" (" + cus_ID +
      "," + cus_LName +
      "," + cus_FName +
      "," + cus_Address +
      "," + cus_Tel + ")";
      //these are the attributes of the
      //customer business object.
      strValues= " Values ( '" + cus.cusID +
      "' , '" + cus.LName +
      "' , '" + cus.FName +
      "' , '" + cus.Address +
      "' , '" + cus.Tel + "' )";

      insertStr = strTable + strFIElds + strValues;
      return insertStr;
    }
  }
}

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