前台:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<style type="text/css">![](https://www.cnblogs.com/Images/dot.gif)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
table{
}{
10
font-size:10pt;
11
}
12
</style>
13
</head>
14
<body style="text-align: center">
15
<form id="form1" runat="server">
16
<div style="text-align: center">
17
<table border="1" cellpadding="3" style=" 642pt; text-align: center">
18
<tr>
19
<td colspan="2" style="height: 30px; text-align: center">
20
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
21
ShowSummary="False" />
22
</td>
23
</tr>
24
<tr>
25
<td style=" 73px; height: 30px; text-align: right;">
26
用户编号:</td>
27
<td style=" 139px; text-align: left; height: 30px;">
28
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
29
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtID"
30
Display="Dynamic" ErrorMessage="用户编号不能为空">*</asp:RequiredFieldValidator>
31
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtID"
32
Display="Dynamic" ErrorMessage="该用户已经存在" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
33
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtID"
34
ErrorMessage="数据范围超出" MaximumValue="1000" MinimumValue="1" Type="Integer"></asp:RangeValidator></td>
35
</tr>
36
<tr>
37
<td style=" 73px; text-align: right;">
38
用户名:</td>
39
<td style=" 139px; text-align: left">
40
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
41
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtName"
42
Display="Dynamic" ErrorMessage="用户名不能为空">*</asp:RequiredFieldValidator></td>
43
</tr>
44
<tr>
45
<td style=" 73px; text-align: right;">
46
性别:</td>
47
<td style=" 139px; text-align: left">
48
<asp:RadioButton ID="rdnNan" runat="server" Checked="True" GroupName="sex" Text="男" />
49
<asp:RadioButton ID="rdnNv" runat="server" GroupName="sex" Text="女" /></td>
50
</tr>
51
<tr>
52
<td colspan="2" style="height: 27px">
53
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
54
<asp:Button ID="btnUpdate" runat="server" Text="修改" OnClick="btnUpdate_Click" />
55
<asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" />
56
<asp:Button ID="btnSel" runat="server" Text="查询" /></td>
57
</tr>
58
</table>
59
60
</div>
61
<br />
62
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966"
63
BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="231px" Width="572px">
64
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
65
<RowStyle BackColor="White" ForeColor="#330099" />
66
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
67
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
68
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
69
</asp:GridView>
70
</form>
71
</body>
72
</html>
73![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
后台:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
public partial class _Default : System.Web.UI.Page
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
13
protected void Page_Load(object sender, EventArgs e)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
this.fillDg();
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
private void fillDg()
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
this.GridView1.DataSource = personOperate.selectAllUser();
21
this.GridView1.DataBind();
22
}
23
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
int pID = Convert.ToInt32(args.Value);
26
if(personOperate.searchPerson(pID))
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
//找到该用户,用户已经存在,则该控件不能通过验证。
29
args.IsValid = false;
30
//Response.Write("用户已经存在!");
31
}
32
else
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
args.IsValid = true;
35
//Response.Write("可以添加");
36
}
37
}
38
protected void btnAdd_Click(object sender, EventArgs e)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
if(this.IsValid)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
//如果此控件通过验证,则添加操作
43
person p = new person();
44
p.pID = Convert.ToInt32(this.txtID.Text);
45
p.pName = this.txtName.Text.ToString();
46
if(this.rdnNan.Checked)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
p.pSex = "男";
49
}
50
else
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
p.pSex = "女";
53
}
54
55
if (personOperate.AddPerson(p))
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
Response.Write("用户添加成功!!!");
58
}
59
else
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
Response.Write("插入操作失败!!");
62
}
63
}
64
this.fillDg();
65
}
66
protected void btnUpdate_Click(object sender, EventArgs e)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
person p = new person();
69
p.pID = Convert.ToInt32(this.txtID.Text);
70
p.pName = this.txtName.Text;
71
if(this.rdnNan.Checked)
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
p.pSex = "男";
74
}
75
else
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
p.pSex = "女";
78
}
79
if(personOperate.updatePerson(p))
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
Response.Write("修改用户资料成功!");
82
}
83
else
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
Response.Write("更新失败!!");
86
}
87
//修改好后重新绑定 GridView
88
this.fillDg();
89
}
90
protected void btnDel_Click(object sender, EventArgs e)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
person p = new person();
93
p.pID = Convert.ToInt32(this.txtID.Text);
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
if (personOperate.delPerson(p))
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
Response.Write("删除成功!");
98
}
99
else
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
Response.Write("删除失败!!");
102
}
103
//修改好后重新绑定 GridView
104
this.fillDg();
105
}
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
实体类person.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
12
/// person 的摘要说明
13
/// </summary>
14
public class person
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
16
public int pID;
17
public string pName;
18
public string pSex;
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
public person()
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
//
23
// TODO: 在此处添加构造函数逻辑
24
//
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
操作类 personOperate.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Data.SqlClient;
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
12
/// personOperate 的摘要说明
13
/// </summary>
14
public class personOperate
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
16
public personOperate()
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
//
19
// TODO: 在此处添加构造函数逻辑
20
//
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
//链接数据库
25
public static SqlConnection createCon()
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
return new SqlConnection("server=localhost;database=login;uid=sa;pwd=admin");
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
//查找该用户是否已经存在
32
public static bool searchPerson(int pID)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
SqlConnection con = personOperate.createCon();
35
con.Open();
36
SqlCommand cmd = new SqlCommand("select personID from person where personID="+pID,con);
37
int count = Convert.ToInt32(cmd.ExecuteScalar());//这里用 ExecuteScalar()方法,而我刚才用了cmd.ExecuteNonQuery()就始终不对。
38
if(count>0)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
return true; //找到该用户,该用户已经存在;
41
}
42
else
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
return false; //找不到该用户,可以添加
45
}
46
}
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
//GridView,呈现数据库中所有的用户
49
public static DataTable selectAllUser() //注意类型是 DataTable
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
SqlConnection con = personOperate.createCon();
52
SqlDataAdapter sdr = new SqlDataAdapter();
53
sdr.SelectCommand = new SqlCommand("select * from person",con);
54
DataSet ds = new DataSet();
55
sdr.Fill(ds,"person");
56
return ds.Tables["person"];
57
}
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
//添加用户
62
public static bool AddPerson(person p)//注意传入参数
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
try
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
SqlConnection con = personOperate.createCon();
67
con.Open();
68
SqlCommand cmd = new SqlCommand("insert into person values("+p.pID+",'"+p.pName+"','"+p.pSex+"')",con);
69
cmd.ExecuteNonQuery();
70
return true;
71
}
72
catch(Exception e)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
return false;
75
}
76
}
77![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
//修改数据
79
public static bool updatePerson(person p)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
try
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
SqlConnection con = personOperate.createCon();
84
con.Open();
85
SqlCommand cmd = new SqlCommand("update person set personName='"+p.pName+"',personSex='"+p.pSex+"' where personID="+p.pID,con);
86
cmd.ExecuteNonQuery();
87
return true;
88
}
89
catch(Exception e)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
return false;
92
}
93
}
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
//删除用户资料
96
public static bool delPerson(person p)
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
98
try
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
SqlConnection con = personOperate.createCon();
101
con.Open();
102
SqlCommand cmd = new SqlCommand("delete from person where personID=" + p.pID, con);
103
cmd.ExecuteNonQuery();
104
return true;
105
}
106
catch(Exception e)
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
return false;
109
}
110
}
111![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
//查询用户资料
113
public static selectPerson(person p)
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
try
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
SqlConnection con = personOperate.createCon();
118
con.Open();
119
SqlCommand cmd = new SqlCommand();
120
//cmd.ExecuteNonQuery();
121
SqlDataReader sdr = cmd.ExecuteReader("select * from person where personID=" + p.pID, con);
122
if (sdr.Read)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
125
}
126
else
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
129
}
130
}
131
catch(Exception e)
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
return false;
134
}
135
}
136
}
137![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
昨天晚上一直搞的很郁闷,不知道在哪个地方出错,一直找不出问题所在
今天终于解决问题了,HOHO~~