首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

教你在c#中实现3层架构


    数据访问层

    数据层包括处理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] [3] 

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航