小 牛

博客园 首页 新随笔 联系 订阅 管理
 1         典型三层  

数据表-----------> common类库-------> dataAccess 类库--------> business类库-------->web表示

 思想:首先设计好数据表,

            然后将表映射成common(数据缓存在内存中)

           其次编写数据访问层dataAccess,访问数据库的参数从common类中取得,而common类中参数的值从      WEB 层中取得

            再写business层,该层访问dataAccess

            最后编写web,该层不能直接访问dataAccess层,而是通过business层来实现的

 

分析:

 首先设计好数据表

                                         (表名mrBranch:字段BranchIDBranchNameSimpleCodeDelFlag

然后将表映射成common类库的MrBranchData (数据缓存在内)

       public class MrBranchData:DataSet//注意该类继承自DataSet类的作用

     {

         public const string MRBRANCH_TABLE_NAME="mrBranch";//大小写,常量字段全部采用大写

         public const string BRANCHID_FIELD="BranchID";

         public const string BRANCHNAME_FIELD="BranchName";

         public const string SIMPLECODE_FIELD="SimpleCode";

         public const string DELFLAG_FIELD="DelFlag";  

         //默认的构造器

         public MrBranchData()

         {

             BuildDataTables(); //调用方法

         }

//生成内存表方法

         private void BuildDataTables(){

              DataTable         table   = new DataTable(MRBRANCH_TABLE_NAME);//定义类存中的表 类型

              DataColumnCollection columns = table.Columns; //该表的列的集合

                           

              columns.Add(BRANCHID_FIELD,typeof(System.Int16));

              columns.Add(BRANCHID_FIELD,typeof(System.String));

              columns.Add(SIMPLECODE_FIELD,typeof(System.String));

              columns.Add(DELFLAG_FIELD,typeof(System.String));

 

              this.Tables.Add(table);//添加到DataSet.Tables

         }

其次编写数据访问层dataAccess,访问数据库的参数从common类中取得,而common类中参数的值从WEB层中取得(web传给business,business传给dataAccess

Using  xx.Common;              //注意要引用common类库

public class MrBranch

     {    private string conStr ;

         private SqlConnection con ;

         private SqlDataAdapter commandAdp ;

         public MrBranch()//构造器

         {   conStr = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString");

              con = new SqlConnection(conStr);

              commandAdp = new SqlDataAdapter();            

         }

          private string paramChg(string str)//写方法下面用到

         {

              str = "@"+str;

              return str;

         }

//数据访问层的实现

public bool InsertMrBranch(string branchName,string simpleCode)

         {    SqlCommand command = new SqlCommand();

              command.CommandText = "InsertMrBranch";

              command.Connection = con;

              command.CommandType = CommandType.StoredProcedure;   //proc

         //proc的参数从commonMrBranchData中的BRANCHNAME_FIELD字段获得

              command.Parameters.Add(paramChg(MrBranchData.BRANCHNAME_FIELD),SqlDbType.VarChar);

              command.Parameters.Add(paramChg(MrBranchData.SIMPLECODE_FIELD),SqlDbType.VarChar);

         //而类MrBranchData中的BRANCHNAME_FIELD字段的值从本方法的参数branchName获得,实际上本方法的参数是WEB层传过来的(WEB层的值传给business,business再调用本方法并且传参数给本方法)

              command.Parameters[paramChg(MrBranchData.BRANCHNAME_FIELD)].Value = branchName;

              command.Parameters[paramChg(MrBranchData.SIMPLECODE_FIELD)].Value = simpleCode;

 

              con.Open();

              int result = command.ExecuteNonQuery();

              if(result>0)

              {     return true;

              }

              else

              {    return false;

              }

         }

再写business层,该层访问dataAccess层(实际上本人编写时,一般是先写web再写business去访问dataAccess

using xx.Common;

     using xx.DataAccess;//注意引用类库

public class Branch

     {

public bool InsertBranch(string txtName ,string txtSimCode)//web层的值传给该方法

         {

              bool result;

              MrBranch DataAccess = new MrBranch();//实例化数据访问层的类

              result = DataAccess.InsertMrBranch(txtName,txtSimCode);//传参,调用该类的方法

              return result;

         }

}

最后编写web,该层不能直接访问dataAccess层,而是通过business层来实现

using xx.Common;

using xx.dataAccess;

using xx.Business;

 

private void cmdAdd_Click(object sender, System.EventArgs e)//web层控件事件

         {   

//实例化businees层的Branch类,并且调用该类的InsertBrach方法,且传web层的值做参数

            bool result = (new Branch()).InsertBranch(txtName.Text,txtSimCode.Text);

              dgdBranch.DataBind();

         }

未完。。。。。。

posted on 2006-07-12 14:41  csu02  阅读(356)  评论(0编辑  收藏  举报