总结学习的程序结构

1 典型三层

数据表-----------&gt common类库-------&gt dataAccess 类库--------&gt business类库--------&gtweb表示

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

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

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

再写business层,该层访问dataAccess层

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

分析:

首先设计好数据表

(表名mrBranch:字段BranchID、BranchName、SimpleCode、DelFlag)

然后将表映射成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的参数从common类MrBranchData中的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();

}

未完。。。。。。

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8690328/viewspace-891765/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8690328/viewspace-891765/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值