数据层包括处理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; } } } |

