asp.net-N层代码示例
一、数据库Test,
表:
createtableCustomers
(
CustIdintIDENTITY(1,1)primarykey,
CustNamevarchar(20)notnull,
Addressvarchar(50),
Linkmanvarchar(20)
)
//insertintoCusomersvalues(''ggg'',''xuzhou'',''zhangsan'');
二、配置文件
web.config
xmlversion="1.0"?>
三、业务实体
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Text;
4
5namespacecom.Model
6{
7/**////
8///业务实体CustomerInfo
9/// 10[Serializable]
11publicclassCustomerInfo
12{
13
14/**////
15///默认的构造函数
16/// 17publicCustomerInfo(){}
18
19/**////
20///有参数的构造函数
21/// 22///
客户号
23///客户名称
24///客户地址
25///联系人
26publicCustomerInfo(intcustId,stringcustName,stringaddress,stringlinkman)
27{
28
29this.custId=custId;
30this.custName=custName;
31this.address=address;
32this.linkman=linkman;
33}
34privateintcustId;
35publicintCustId
36{
37get{returncustId;}
38set{custId=value;}
39}
40
41privatestringcustName;
42publicstringCustName
43{
44get{returncustName;}
45set{custName=value;}
46}
47
48privatestringaddress;
49publicstringAddress
50{
51get{returnaddress;}
52set{address=value;}
53}
54
55privatestringlinkman;
56publicstringLinkman
57{
58get{returnlinkman;}
59set{linkman=value;}
60}
61}
62}
63
四、数据访问层
类:SqlHelper(不多写)
类:Customer
1usingSystem;
2usingSystem.Data.SqlClient;
3usingSystem.Data;
4usingSystem.Text;
5usingSystem.Collections.Generic;
6usingcom.Model;
7
8namespacecom.DataAccess
9{
10/**////
11///对客户表的所有数据访问操作
12///
13publicclassCustomer
14{
15
16//静态常量,参数名,T-SQL串
17privateconststringSQL_SELECT_CUSTOMER_BY_ID=
18"SELECTCustId,CustName,Address,LinkmanFROMCUSTOMERSWHERECustID=@CustId";
19privateconststringSQL_SELECT_CUSTOMER_BY_NAME=
20"SELECTCustId,CustName,Address,LinkmanFROMCUSTOMERSWHERECustName=@CustName";
21privateconststringSQL_SELECT_CUSTOMER_BY_ALL=
22"SELECTCustId,CustName,Address,LinkmanFROMCUSTOMERS";
23privateconststringSQL_UPDATE_CUSTOMER_BY_ID=
24"UPDATECUSTOMERSSETCustName=@CustName,Address=@Address,Linkman=@LinkmanWHERECustId=@CustId";
25privateconststringSQL_DELETE_CUSTOMER_BY_ID=
26"DELETECUSTOMERSWHERECustId=@CustId";
27privateconststringSQL_INSERT_CUSTOMER=
28"Declare@IDint;INSERTINTOCUSTOMERSVALUES(@CustName,@Address,@Linkman);SELECT@ID=@@IDENTITY;SELECT@ID";
29
30privateconststringPARM_CUSTOMERID="@CustId";
31privateconststringPARM_CUSTOMERNAME="@CustName";
32privateconststringPARM_ADDRESS="@Address";
33privateconststringPARM_LINKMAN="@Linkman";
34
35/**////
36///按客户ID查询
37///
38///客户号
39///客户对象
40publicCustomerInfoGetCustomerById(intcustId)
41{
42CustomerInfocustomerInfo=null;
43SqlParameterparm=newSqlParameter(PARM_CUSTOMERID,SqlDbType.Int);
44parm.Value=custId;
45
46//按客户号参数执行查询得到一个客户信息
47using(SqlDataReaderrdr=SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,SQL_SELECT_CUSTOMER_BY_ID,parm))
48{
49