惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
D
Docker
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
博客园 - 聂微东
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
爱范儿
爱范儿
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
Recent Announcements
Recent Announcements
U
Unit 42
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
量子位
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - csu02

erp asp+com 删除表的一列 更改列名 分页 千万数量级分页存储过程 XML的五种用途~ 表的列数 列名 得到一个省份,城市数据库 window.open()的所有参数列表 存儲過程解密SQL 分页控件 返回存儲過程的多個輸出字段 SqlCommand 与 SqlDataAdapter 产生一个int数组,长度为100,并向其中随机插入1-100 dataGrid使用 绑定 定向到Url 几个系统 利用IFRAME 让每个页面都继承菜单控 不是用户自定义控件来实现的 C#事件机制学习 完成单元测试
学到的程序结构
csu02 · 2006-07-12 · via 博客园 - csu02
 

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

         }

未完。。。。。。