asp.net-N层代码示例
2008-05-11 22:17
232 查看
一、数据库Test,
表:
create table Customers
(
CustId int IDENTITY(1,1) primary key,
CustName varchar(20) not null,
Address varchar(50),
Linkman varchar(20)
)
//insert into Cusomers values('ggg','xuzhou','zhangsan');
二、配置文件web.config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="TestConnectionString" connectionString="Data Source=GONGCHL;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
三、业务实体
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace com.Model
6
{
7
/**//// <summary>
8
/// 业务实体CustomerInfo
9
/// </summary>
10
[Serializable]
11
public class CustomerInfo
12
{
13
14
/**//// <summary>
15
/// 默认的构造函数
16
/// </summary>
17
public CustomerInfo()
{}
18
19
/**//// <summary>
20
/// 有参数的构造函数
21
/// </summary>
22
/// <param name="custId">客户号</param>
23
/// <param name="custName">客户名称</param>
24
/// <param name="address">客户地址</param>
25
/// <param name="linkman">联系人</param>
26
public CustomerInfo(int custId, string custName, string address, string linkman)
27
{
28
29
this.custId = custId;
30
this.custName = custName;
31
this.address = address;
32
this.linkman = linkman;
33
}
34
private int custId;
35
public int CustId
36
{
37
get
{ return custId; }
38
set
{ custId = value; }
39
}
40
41
private string custName;
42
public string CustName
43
{
44
get
{ return custName; }
45
set
{ custName = value; }
46
}
47
48
private string address;
49
public string Address
50
{
51
get
{ return address; }
52
set
{ address = value; }
53
}
54
55
private string linkman;
56
public string Linkman
57
{
58
get
{ return linkman; }
59
set
{ linkman = value; }
60
}
61
}
62
}
63
四、数据访问层
类:SqlHelper(不多写)
类:Customer
1
using System;
2
using System.Data.SqlClient;
3
using System.Data;
4
using System.Text;
5
using System.Collections.Generic;
6
using com.Model;
7
8
namespace com.DataAccess
9
{
10
/**//// <summary>
11
/// 对客户表的所有数据访问操作
12
/// </summary>
13
public class Customer
14
{
15
16
//静态常量,参数名,T-SQL串
17
private const string SQL_SELECT_CUSTOMER_BY_ID =
18
"SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS WHERE CustID = @CustId";
19
private const string SQL_SELECT_CUSTOMER_BY_NAME =
20
"SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS WHERE CustName = @CustName";
21
private const string SQL_SELECT_CUSTOMER_BY_ALL =
22
"SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS";
23
private const string SQL_UPDATE_CUSTOMER_BY_ID =
24
"UPDATE CUSTOMERS SET CustName=@CustName, Address=@Address, Linkman = @Linkman WHERE CustId=@CustId ";
25
private const string SQL_DELETE_CUSTOMER_BY_ID =
26
"DELETE CUSTOMERS WHERE CustId=@CustId ";
27
private const string SQL_INSERT_CUSTOMER =
28
"Declare @ID int;INSERT INTO CUSTOMERS VALUES(@CustName, @Address, @Linkman);SELECT @ID = @@IDENTITY; SELECT @ID";
29
30
private const string PARM_CUSTOMERID = "@CustId";
31
private const string PARM_CUSTOMERNAME = "@CustName";
32
private const string PARM_ADDRESS = "@Address";
33
private const string PARM_LINKMAN = "@Linkman";
34
35
/**//// <summary>
36
/// 按客户ID查询
37
/// </summary>
38
/// <param name="custId">客户号</param>
39
/// <returns>客户对象</returns>
40
public CustomerInfo GetCustomerById(int custId)
41
{
42
CustomerInfo customerInfo=null;
43
SqlParameter parm = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
44
parm.Value = custId;
45
46
//按客户号参数执行查询得到一个客户信息
47
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_ID, parm))
48
{
49
if (rdr.Read())
50
customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
51
}
52
return customerInfo;
53
}
54
55
/**//// <summary>
56
/// 按客户名称查询
57
/// </summary>
58
/// <param name="custName">客户名称</param>
59
/// <returns>客户对象</returns>
60
public CustomerInfo GetCustomerByName(string custName)
61
{
62
CustomerInfo customerInfo = null;
63
SqlParameter parm = new SqlParameter(PARM_CUSTOMERNAME, SqlDbType.VarChar,20);
64
parm.Value = custName;
65
66
//按客户号参数执行查询得到一个客户信息
67
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_NAME, parm))
68
{
69
if (rdr.Read())
70
customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
71
}
72
return customerInfo;
73
}
74
75
/**//// <summary>
76
/// 查询所有客户信息
77
/// 结果为IList
78
/// </summary>
79
/// <returns>一个客户集合</returns>
80
public IList<CustomerInfo> GetCusomersByAll()
81
{
82
83
IList<CustomerInfo> customers = new List<CustomerInfo>();
84
85
//Finally execute the query
86
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_ALL, null))
87
{
88
while (rdr.Read())
89
{
90
CustomerInfo customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
91
customers.Add(customerInfo);
92
}
93
}
94
95
return customers;
96
}
97
/**//// <summary>
98
/// 插入一个客户信息
99
/// </summary>
100
/// <param name="customer">客户对象CustomerInfo</param>
101
/// <returns>bool类型,true or false</returns>
102
public bool InsertCustomer(CustomerInfo customerInfo)
103
{
104
SqlParameter[] paras = new SqlParameter[3];
105
paras[0]=new SqlParameter(PARM_CUSTOMERNAME,SqlDbType.VarChar,20);
106
paras[0].Value=customerInfo.CustName;
107
paras[1]=new SqlParameter(PARM_ADDRESS,SqlDbType.VarChar,50);
108
paras[1].Value=customerInfo.Address;
109
paras[2]=new SqlParameter(PARM_LINKMAN,SqlDbType.VarChar,20);
110
paras[2].Value=customerInfo.Linkman;
111
112
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_INSERT_CUSTOMER, paras))
113
{
114
if (rdr.Read())
115
customerInfo.CustId = rdr.GetInt32(0);
116
else
117
return false;
118
}
119
return true;
120
}
121
122
/**//// <summary>
123
/// 修改一个客户信息
124
/// </summary>
125
/// <param name="customer">客户对象CustomerInfo</param>
126
/// <returns>bool类型,true or false</returns>
127
public bool UpdateCustomerByID(CustomerInfo customerInfo)
128
{
129
SqlParameter[] paras = new SqlParameter[4];
130
paras[0] = new SqlParameter(PARM_CUSTOMERNAME, SqlDbType.VarChar, 20);
131
paras[0].Value = customerInfo.CustName;
132
paras[1] = new SqlParameter(PARM_ADDRESS, SqlDbType.VarChar, 50);
133
paras[1].Value = customerInfo.Address;
134
paras[2] = new SqlParameter(PARM_LINKMAN, SqlDbType.VarChar, 20);
135
paras[2].Value = customerInfo.Linkman;
136
paras[3] = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
137
paras[3].Value = customerInfo.CustId;
138
139
int row = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_UPDATE_CUSTOMER_BY_ID, paras);
140
if (row == 0)
141
return false;
142
return true;
143
}
144
145
/**//// <summary>
146
/// 按ID删除一个客户信息
147
/// </summary>
148
/// <param name="custId">客户号</param>
149
/// <returns>bool类型,true or false</returns>
150
public bool DeleteCustomerByID(int custId)
151
{
152
SqlParameter para = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
153
para.Value = custId;
154
155
int row = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_DELETE_CUSTOMER_BY_ID, para);
156
if (row == 0)
157
return false;
158
return true;
159
}
160
161
}
162
}
五、业务逻辑层
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using com.DataAccess;
5
using com.Model;
6
using com.BusinessRule;
7
8
namespace com.BusinessLogic
9
{
10
public class CustomerLogic
11
{
12
/**//// <summary>
13
/// 插入一个客户信息
14
/// </summary>
15
/// <param name="custId">客户号</param>
16
/// <param name="custName">客户名称</param>
17
/// <param name="address">客户地址</param>
18
/// <param name="linkman">联系人</param>
19
/// <returns>bool类型,true or false</returns>
20
public bool InsertCustomer(int custId,string custName, string address, string linkman)
21
{
22
if (CustomerRule.IsExistCustomerName(custName))
23
return false;
24
Customer customer = new Customer();
25
CustomerInfo customerInfo = new CustomerInfo(custId,custName,address,linkman);
26
return customer.InsertCustomer(customerInfo);
27
}
28
29
/**//// <summary>
30
/// 插入一个客户信息
31
/// </summary>
32
/// <param name="custName">客户名称</param>
33
/// <param name="address">客户地址</param>
34
/// <param name="linkman">联系人</param>
35
/// <returns>bool类型,true or false</returns>
36
public bool InsertCustomer(string custName, string address, string linkman)
37
{
38
if (CustomerRule.IsExistCustomerName(custName))
39
return false;
40
Customer customer = new Customer();
41
CustomerInfo customerInfo = new CustomerInfo(0, custName, address, linkman);
42
return customer.InsertCustomer(customerInfo);
43
}
44
45
/**//// <summary>
46
/// 修改一个客户信息
47
/// </summary>
48
/// <param name="custId">客户号</param>
49
/// <param name="custName">客户名称</param>
50
/// <param name="address">客户地址</param>
51
/// <param name="linkman">联系人</param>
52
/// <returns>bool类型,true or false</returns>
53
public bool UpdateCustomer(int custId,string custName, string address, string linkman)
54
{
55
Customer customer = new Customer();
56
CustomerInfo customerInfo = new CustomerInfo(custId, custName, address, linkman);
57
return customer.UpdateCustomerByID(customerInfo);
58
}
59
60
/**//// <summary>
61
/// 按ID删除一个客户信息
62
/// </summary>
63
/// <param name="custId">客户号</param>
64
/// <returns>bool类型,true or false</returns>
65
public bool DeleteCustomerByID(int custId)
66
{
67
Customer customer = new Customer();
68
return customer.DeleteCustomerByID(custId);
69
}
70
71
72
/**//// <summary>
73
/// 查询所有客户信息
74
/// 结果为IList
75
/// </summary>
76
/// <returns>一个客户集合</returns>
77
public IList<CustomerInfo> GetCustomersByAll()
78
{
79
Customer customer = new Customer();
80
return customer.GetCusomersByAll();
81
}
82
}
83
}
84
六、业务规则层
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using com.DataAccess;
5
using com.Model;
6
7
namespace com.BusinessRule
8
{
9
/**//// <summary>
10
/// 检查客户信息的合法性
11
/// </summary>
12
public class CustomerRule
13
{
14
/**//// <summary>
15
/// 检查客户的名称是否已经存在
16
/// </summary>
17
/// <remarks>
18
/// e.g.:
19
/// bool exist =CustomerRule.IsExistCustomerName(custName);
20
/// </remarks>
21
/// <param name="custName">客户名称</param>
22
/// <returns>客户存在与否</returns>
23
24
public static bool IsExistCustomerName(string custName)
25
{
26
Customer cust = new Customer();
27
CustomerInfo custInfo = cust.GetCustomerByName(custName);
28
if (custInfo == null)
29
return false;
30
else
31
return true;
32
}
33
}
34
}
35
七、业务外观层
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web.UI.WebControls;
5
using com.BusinessLogic;
6
using com.Model;
7
8
namespace com.BusinessFacade
9
{
10
/**//// <summary>
11
/// 为界面中Table处理数据
12
/// </summary>
13
public class CustomerTable
14
{
15
public static void SetTableData(Table table)
16
{
17
IList<CustomerInfo> list = new CustomerLogic().GetCustomersByAll();
18
19
AddRowHead(table);
20
foreach (CustomerInfo cust in list)
21
{
22
AddRow(table, cust);
23
}
24
}
25
26
private static void AddRowHead(Table table)
27
{
28
TableCell cell = new TableCell();
29
cell.Text = "Head";
30
TableRow row = new TableRow();
31
row.Cells.Add(cell);
32
table.Rows.Add(row);
33
34
}
35
private static void AddRow(Table table, CustomerInfo cust)
36
{
37
TableRow row = new TableRow();
38
TableCell cell1 = new TableCell();
39
cell1.Text = cust.CustId.ToString();
40
TableCell cell2 = new TableCell();
41
cell2.Text = cust.CustName;
42
TableCell cell3 = new TableCell();
43
cell3.Text = cust.Address;
44
TableCell cell4 = new TableCell();
45
cell4.Text = cust.Linkman;
46
row.Cells.Add(cell1);
47
row.Cells.Add(cell2);
48
row.Cells.Add(cell3);
49
row.Cells.Add(cell4);
50
51
table.Rows.Add(row);
52
}
53
}
54
}
55
八、界面层
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
</div>
13
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
14
InsertMethod="InsertCustomer" SelectMethod="GetCustomersByAll" TypeName="com.BusinessLogic.CustomerLogic" DeleteMethod="DeleteCustomerByID" UpdateMethod="UpdateCustomer">
15
<DeleteParameters>
16
<asp:ControlParameter ControlID="FormView1" PropertyName="SelectedValue" Name="custId" Type="Int32" />
17
</DeleteParameters>
18
<UpdateParameters>
19
<asp:Parameter Name="custId" Type="Int32" />
20
<asp:Parameter Name="custName" Type="String" />
21
<asp:Parameter Name="address" Type="String" />
22
<asp:Parameter Name="linkman" Type="String" />
23
</UpdateParameters>
24
<InsertParameters>
25
<asp:Parameter Name="custName" Type="String" />
26
<asp:Parameter Name="address" Type="String" />
27
<asp:Parameter Name="linkman" Type="String" />
28
</InsertParameters>
29
</asp:ObjectDataSource>
30
<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" AllowPaging="True" DataKeyNames="custId">
31
<EditItemTemplate>
32
CustName:
33
<asp:TextBox ID="CustNameTextBox" runat="server" Text='<%# Bind("CustName") %>'></asp:TextBox><br />
34
Address:
35
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox><br />
36
Linkman:
37
<asp:TextBox ID="LinkmanTextBox" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox><br />
38
CustId:
39
<asp:TextBox ID="CustIdTextBox" runat="server" BorderStyle="None" Enabled="False"
40
Text='<%# Bind("CustId") %>'></asp:TextBox><br />
41
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
42
Text="更新"></asp:LinkButton>
43
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
44
Text="取消"></asp:LinkButton>
45
</EditItemTemplate>
46
<InsertItemTemplate>
47
CustName:
48
<asp:TextBox ID="CustNameTextBox" runat="server" Text='<%# Bind("CustName") %>'></asp:TextBox><br />
49
Address:
50
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox><br />
51
Linkman:
52
<asp:TextBox ID="LinkmanTextBox" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox><br />
53
CustId:
54
<asp:TextBox ID="CustIdTextBox" runat="server" Text='0' Enabled="False"></asp:TextBox><br />
55
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
56
Text="插入"></asp:LinkButton>
57
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
58
Text="取消"></asp:LinkButton>
59
</InsertItemTemplate>
60
<ItemTemplate>
61
CustName:
62
<asp:Label ID="CustNameLabel" runat="server" Text='<%# Bind("CustName") %>'></asp:Label><br />
63
Address:
64
<asp:Label ID="AddressLabel" runat="server" Text='<%# Bind("Address") %>'></asp:Label><br />
65
Linkman:
66
<asp:Label ID="LinkmanLabel" runat="server" Text='<%# Bind("Linkman") %>'></asp:Label><br />
67
CustId:
68
<asp:Label ID="CustIdLabel" runat="server" Enabled="False" Text='<%# Bind("CustId") %>'></asp:Label><br />
69
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
70
Text="编辑"></asp:LinkButton>
71
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
72
Text="删除" ></asp:LinkButton>
73
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
74
Text="新建"></asp:LinkButton>
75
</ItemTemplate>
76
</asp:FormView>
77
<asp:Table ID="Table1" runat="server">
78
</asp:Table>
79
80
</form>
81
</body>
82
</html>
表:
create table Customers
(
CustId int IDENTITY(1,1) primary key,
CustName varchar(20) not null,
Address varchar(50),
Linkman varchar(20)
)
//insert into Cusomers values('ggg','xuzhou','zhangsan');
二、配置文件web.config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="TestConnectionString" connectionString="Data Source=GONGCHL;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
三、业务实体
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace com.Model
6
{
7
/**//// <summary>
8
/// 业务实体CustomerInfo
9
/// </summary>
10
[Serializable]
11
public class CustomerInfo
12
{
13
14
/**//// <summary>
15
/// 默认的构造函数
16
/// </summary>
17
public CustomerInfo()
{}
18
19
/**//// <summary>
20
/// 有参数的构造函数
21
/// </summary>
22
/// <param name="custId">客户号</param>
23
/// <param name="custName">客户名称</param>
24
/// <param name="address">客户地址</param>
25
/// <param name="linkman">联系人</param>
26
public CustomerInfo(int custId, string custName, string address, string linkman)
27
{
28
29
this.custId = custId;
30
this.custName = custName;
31
this.address = address;
32
this.linkman = linkman;
33
}
34
private int custId;
35
public int CustId
36
{
37
get
{ return custId; }
38
set
{ custId = value; }
39
}
40
41
private string custName;
42
public string CustName
43
{
44
get
{ return custName; }
45
set
{ custName = value; }
46
}
47
48
private string address;
49
public string Address
50
{
51
get
{ return address; }
52
set
{ address = value; }
53
}
54
55
private string linkman;
56
public string Linkman
57
{
58
get
{ return linkman; }
59
set
{ linkman = value; }
60
}
61
}
62
}
63
四、数据访问层
类:SqlHelper(不多写)
类:Customer
1
using System;
2
using System.Data.SqlClient;
3
using System.Data;
4
using System.Text;
5
using System.Collections.Generic;
6
using com.Model;
7
8
namespace com.DataAccess
9
{
10
/**//// <summary>
11
/// 对客户表的所有数据访问操作
12
/// </summary>
13
public class Customer
14
{
15
16
//静态常量,参数名,T-SQL串
17
private const string SQL_SELECT_CUSTOMER_BY_ID =
18
"SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS WHERE CustID = @CustId";
19
private const string SQL_SELECT_CUSTOMER_BY_NAME =
20
"SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS WHERE CustName = @CustName";
21
private const string SQL_SELECT_CUSTOMER_BY_ALL =
22
"SELECT CustId, CustName, Address, Linkman FROM CUSTOMERS";
23
private const string SQL_UPDATE_CUSTOMER_BY_ID =
24
"UPDATE CUSTOMERS SET CustName=@CustName, Address=@Address, Linkman = @Linkman WHERE CustId=@CustId ";
25
private const string SQL_DELETE_CUSTOMER_BY_ID =
26
"DELETE CUSTOMERS WHERE CustId=@CustId ";
27
private const string SQL_INSERT_CUSTOMER =
28
"Declare @ID int;INSERT INTO CUSTOMERS VALUES(@CustName, @Address, @Linkman);SELECT @ID = @@IDENTITY; SELECT @ID";
29
30
private const string PARM_CUSTOMERID = "@CustId";
31
private const string PARM_CUSTOMERNAME = "@CustName";
32
private const string PARM_ADDRESS = "@Address";
33
private const string PARM_LINKMAN = "@Linkman";
34
35
/**//// <summary>
36
/// 按客户ID查询
37
/// </summary>
38
/// <param name="custId">客户号</param>
39
/// <returns>客户对象</returns>
40
public CustomerInfo GetCustomerById(int custId)
41
{
42
CustomerInfo customerInfo=null;
43
SqlParameter parm = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
44
parm.Value = custId;
45
46
//按客户号参数执行查询得到一个客户信息
47
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_ID, parm))
48
{
49
if (rdr.Read())
50
customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
51
}
52
return customerInfo;
53
}
54
55
/**//// <summary>
56
/// 按客户名称查询
57
/// </summary>
58
/// <param name="custName">客户名称</param>
59
/// <returns>客户对象</returns>
60
public CustomerInfo GetCustomerByName(string custName)
61
{
62
CustomerInfo customerInfo = null;
63
SqlParameter parm = new SqlParameter(PARM_CUSTOMERNAME, SqlDbType.VarChar,20);
64
parm.Value = custName;
65
66
//按客户号参数执行查询得到一个客户信息
67
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_NAME, parm))
68
{
69
if (rdr.Read())
70
customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
71
}
72
return customerInfo;
73
}
74
75
/**//// <summary>
76
/// 查询所有客户信息
77
/// 结果为IList
78
/// </summary>
79
/// <returns>一个客户集合</returns>
80
public IList<CustomerInfo> GetCusomersByAll()
81
{
82
83
IList<CustomerInfo> customers = new List<CustomerInfo>();
84
85
//Finally execute the query
86
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CUSTOMER_BY_ALL, null))
87
{
88
while (rdr.Read())
89
{
90
CustomerInfo customerInfo = new CustomerInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
91
customers.Add(customerInfo);
92
}
93
}
94
95
return customers;
96
}
97
/**//// <summary>
98
/// 插入一个客户信息
99
/// </summary>
100
/// <param name="customer">客户对象CustomerInfo</param>
101
/// <returns>bool类型,true or false</returns>
102
public bool InsertCustomer(CustomerInfo customerInfo)
103
{
104
SqlParameter[] paras = new SqlParameter[3];
105
paras[0]=new SqlParameter(PARM_CUSTOMERNAME,SqlDbType.VarChar,20);
106
paras[0].Value=customerInfo.CustName;
107
paras[1]=new SqlParameter(PARM_ADDRESS,SqlDbType.VarChar,50);
108
paras[1].Value=customerInfo.Address;
109
paras[2]=new SqlParameter(PARM_LINKMAN,SqlDbType.VarChar,20);
110
paras[2].Value=customerInfo.Linkman;
111
112
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_INSERT_CUSTOMER, paras))
113
{
114
if (rdr.Read())
115
customerInfo.CustId = rdr.GetInt32(0);
116
else
117
return false;
118
}
119
return true;
120
}
121
122
/**//// <summary>
123
/// 修改一个客户信息
124
/// </summary>
125
/// <param name="customer">客户对象CustomerInfo</param>
126
/// <returns>bool类型,true or false</returns>
127
public bool UpdateCustomerByID(CustomerInfo customerInfo)
128
{
129
SqlParameter[] paras = new SqlParameter[4];
130
paras[0] = new SqlParameter(PARM_CUSTOMERNAME, SqlDbType.VarChar, 20);
131
paras[0].Value = customerInfo.CustName;
132
paras[1] = new SqlParameter(PARM_ADDRESS, SqlDbType.VarChar, 50);
133
paras[1].Value = customerInfo.Address;
134
paras[2] = new SqlParameter(PARM_LINKMAN, SqlDbType.VarChar, 20);
135
paras[2].Value = customerInfo.Linkman;
136
paras[3] = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
137
paras[3].Value = customerInfo.CustId;
138
139
int row = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_UPDATE_CUSTOMER_BY_ID, paras);
140
if (row == 0)
141
return false;
142
return true;
143
}
144
145
/**//// <summary>
146
/// 按ID删除一个客户信息
147
/// </summary>
148
/// <param name="custId">客户号</param>
149
/// <returns>bool类型,true or false</returns>
150
public bool DeleteCustomerByID(int custId)
151
{
152
SqlParameter para = new SqlParameter(PARM_CUSTOMERID, SqlDbType.Int);
153
para.Value = custId;
154
155
int row = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_DELETE_CUSTOMER_BY_ID, para);
156
if (row == 0)
157
return false;
158
return true;
159
}
160
161
}
162
}
五、业务逻辑层
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using com.DataAccess;
5
using com.Model;
6
using com.BusinessRule;
7
8
namespace com.BusinessLogic
9
{
10
public class CustomerLogic
11
{
12
/**//// <summary>
13
/// 插入一个客户信息
14
/// </summary>
15
/// <param name="custId">客户号</param>
16
/// <param name="custName">客户名称</param>
17
/// <param name="address">客户地址</param>
18
/// <param name="linkman">联系人</param>
19
/// <returns>bool类型,true or false</returns>
20
public bool InsertCustomer(int custId,string custName, string address, string linkman)
21
{
22
if (CustomerRule.IsExistCustomerName(custName))
23
return false;
24
Customer customer = new Customer();
25
CustomerInfo customerInfo = new CustomerInfo(custId,custName,address,linkman);
26
return customer.InsertCustomer(customerInfo);
27
}
28
29
/**//// <summary>
30
/// 插入一个客户信息
31
/// </summary>
32
/// <param name="custName">客户名称</param>
33
/// <param name="address">客户地址</param>
34
/// <param name="linkman">联系人</param>
35
/// <returns>bool类型,true or false</returns>
36
public bool InsertCustomer(string custName, string address, string linkman)
37
{
38
if (CustomerRule.IsExistCustomerName(custName))
39
return false;
40
Customer customer = new Customer();
41
CustomerInfo customerInfo = new CustomerInfo(0, custName, address, linkman);
42
return customer.InsertCustomer(customerInfo);
43
}
44
45
/**//// <summary>
46
/// 修改一个客户信息
47
/// </summary>
48
/// <param name="custId">客户号</param>
49
/// <param name="custName">客户名称</param>
50
/// <param name="address">客户地址</param>
51
/// <param name="linkman">联系人</param>
52
/// <returns>bool类型,true or false</returns>
53
public bool UpdateCustomer(int custId,string custName, string address, string linkman)
54
{
55
Customer customer = new Customer();
56
CustomerInfo customerInfo = new CustomerInfo(custId, custName, address, linkman);
57
return customer.UpdateCustomerByID(customerInfo);
58
}
59
60
/**//// <summary>
61
/// 按ID删除一个客户信息
62
/// </summary>
63
/// <param name="custId">客户号</param>
64
/// <returns>bool类型,true or false</returns>
65
public bool DeleteCustomerByID(int custId)
66
{
67
Customer customer = new Customer();
68
return customer.DeleteCustomerByID(custId);
69
}
70
71
72
/**//// <summary>
73
/// 查询所有客户信息
74
/// 结果为IList
75
/// </summary>
76
/// <returns>一个客户集合</returns>
77
public IList<CustomerInfo> GetCustomersByAll()
78
{
79
Customer customer = new Customer();
80
return customer.GetCusomersByAll();
81
}
82
}
83
}
84
六、业务规则层
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using com.DataAccess;
5
using com.Model;
6
7
namespace com.BusinessRule
8
{
9
/**//// <summary>
10
/// 检查客户信息的合法性
11
/// </summary>
12
public class CustomerRule
13
{
14
/**//// <summary>
15
/// 检查客户的名称是否已经存在
16
/// </summary>
17
/// <remarks>
18
/// e.g.:
19
/// bool exist =CustomerRule.IsExistCustomerName(custName);
20
/// </remarks>
21
/// <param name="custName">客户名称</param>
22
/// <returns>客户存在与否</returns>
23
24
public static bool IsExistCustomerName(string custName)
25
{
26
Customer cust = new Customer();
27
CustomerInfo custInfo = cust.GetCustomerByName(custName);
28
if (custInfo == null)
29
return false;
30
else
31
return true;
32
}
33
}
34
}
35
七、业务外观层
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web.UI.WebControls;
5
using com.BusinessLogic;
6
using com.Model;
7
8
namespace com.BusinessFacade
9
{
10
/**//// <summary>
11
/// 为界面中Table处理数据
12
/// </summary>
13
public class CustomerTable
14
{
15
public static void SetTableData(Table table)
16
{
17
IList<CustomerInfo> list = new CustomerLogic().GetCustomersByAll();
18
19
AddRowHead(table);
20
foreach (CustomerInfo cust in list)
21
{
22
AddRow(table, cust);
23
}
24
}
25
26
private static void AddRowHead(Table table)
27
{
28
TableCell cell = new TableCell();
29
cell.Text = "Head";
30
TableRow row = new TableRow();
31
row.Cells.Add(cell);
32
table.Rows.Add(row);
33
34
}
35
private static void AddRow(Table table, CustomerInfo cust)
36
{
37
TableRow row = new TableRow();
38
TableCell cell1 = new TableCell();
39
cell1.Text = cust.CustId.ToString();
40
TableCell cell2 = new TableCell();
41
cell2.Text = cust.CustName;
42
TableCell cell3 = new TableCell();
43
cell3.Text = cust.Address;
44
TableCell cell4 = new TableCell();
45
cell4.Text = cust.Linkman;
46
row.Cells.Add(cell1);
47
row.Cells.Add(cell2);
48
row.Cells.Add(cell3);
49
row.Cells.Add(cell4);
50
51
table.Rows.Add(row);
52
}
53
}
54
}
55
八、界面层
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
</div>
13
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
14
InsertMethod="InsertCustomer" SelectMethod="GetCustomersByAll" TypeName="com.BusinessLogic.CustomerLogic" DeleteMethod="DeleteCustomerByID" UpdateMethod="UpdateCustomer">
15
<DeleteParameters>
16
<asp:ControlParameter ControlID="FormView1" PropertyName="SelectedValue" Name="custId" Type="Int32" />
17
</DeleteParameters>
18
<UpdateParameters>
19
<asp:Parameter Name="custId" Type="Int32" />
20
<asp:Parameter Name="custName" Type="String" />
21
<asp:Parameter Name="address" Type="String" />
22
<asp:Parameter Name="linkman" Type="String" />
23
</UpdateParameters>
24
<InsertParameters>
25
<asp:Parameter Name="custName" Type="String" />
26
<asp:Parameter Name="address" Type="String" />
27
<asp:Parameter Name="linkman" Type="String" />
28
</InsertParameters>
29
</asp:ObjectDataSource>
30
<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" AllowPaging="True" DataKeyNames="custId">
31
<EditItemTemplate>
32
CustName:
33
<asp:TextBox ID="CustNameTextBox" runat="server" Text='<%# Bind("CustName") %>'></asp:TextBox><br />
34
Address:
35
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox><br />
36
Linkman:
37
<asp:TextBox ID="LinkmanTextBox" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox><br />
38
CustId:
39
<asp:TextBox ID="CustIdTextBox" runat="server" BorderStyle="None" Enabled="False"
40
Text='<%# Bind("CustId") %>'></asp:TextBox><br />
41
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
42
Text="更新"></asp:LinkButton>
43
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
44
Text="取消"></asp:LinkButton>
45
</EditItemTemplate>
46
<InsertItemTemplate>
47
CustName:
48
<asp:TextBox ID="CustNameTextBox" runat="server" Text='<%# Bind("CustName") %>'></asp:TextBox><br />
49
Address:
50
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox><br />
51
Linkman:
52
<asp:TextBox ID="LinkmanTextBox" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox><br />
53
CustId:
54
<asp:TextBox ID="CustIdTextBox" runat="server" Text='0' Enabled="False"></asp:TextBox><br />
55
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
56
Text="插入"></asp:LinkButton>
57
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
58
Text="取消"></asp:LinkButton>
59
</InsertItemTemplate>
60
<ItemTemplate>
61
CustName:
62
<asp:Label ID="CustNameLabel" runat="server" Text='<%# Bind("CustName") %>'></asp:Label><br />
63
Address:
64
<asp:Label ID="AddressLabel" runat="server" Text='<%# Bind("Address") %>'></asp:Label><br />
65
Linkman:
66
<asp:Label ID="LinkmanLabel" runat="server" Text='<%# Bind("Linkman") %>'></asp:Label><br />
67
CustId:
68
<asp:Label ID="CustIdLabel" runat="server" Enabled="False" Text='<%# Bind("CustId") %>'></asp:Label><br />
69
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
70
Text="编辑"></asp:LinkButton>
71
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
72
Text="删除" ></asp:LinkButton>
73
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
74
Text="新建"></asp:LinkButton>
75
</ItemTemplate>
76
</asp:FormView>
77
<asp:Table ID="Table1" runat="server">
78
</asp:Table>
79
80
</form>
81
</body>
82
</html>
相关文章推荐
- jsp和asp.net共享session值示例代码
- Asp.Net中的获取Web.config中设置的参数!(前后台的代码示例)
- ASP.NET学习5--N层代码示例
- Asp.net FileUpload+Image制作头像效果示例代码
- asp.net直接Response输出WML页面示例代码
- [转]微软代码示例:ASP.NET 2.0 三层架构应用程序教程系列
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- [代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- AjaxPro.NET框架生成高效率的Tree(Asp.net 2.0)(示例代码下载)
- Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- asp.net产生N位随机图片验证码的完整示例代码
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- [导入]Ajax即时实现服务端数据验证(Asp.net 2.0)(示例代码下载)
- [导入]Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- [代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- 一起谈.NET技术,ASP.NET的地址重写(URLRewriter)实现原理及代码示例