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 <style type="text/css">
9 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
后台:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11public partial class _Default : System.Web.UI.Page
12{
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 this.fillDg();
16 }
17
18 private void fillDg()
19 {
20 this.GridView1.DataSource = personOperate.selectAllUser();
21 this.GridView1.DataBind();
22 }
23 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
24 {
25 int pID = Convert.ToInt32(args.Value);
26 if(personOperate.searchPerson(pID))
27 {
28 //找到该用户,用户已经存在,则该控件不能通过验证。
29 args.IsValid = false;
30 //Response.Write("用户已经存在!");
31 }
32 else
33 {
34 args.IsValid = true;
35 //Response.Write("可以添加");
36 }
37 }
38 protected void btnAdd_Click(object sender, EventArgs e)
39 {
40 if(this.IsValid)
41 {
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 {
48 p.pSex = "男";
49 }
50 else
51 {
52 p.pSex = "女";
53 }
54
55 if (personOperate.AddPerson(p))
56 {
57 Response.Write("用户添加成功!!!");
58 }
59 else
60 {
61 Response.Write("插入操作失败!!");
62 }
63 }
64 this.fillDg();
65 }
66 protected void btnUpdate_Click(object sender, EventArgs e)
67 {
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 {
73 p.pSex = "男";
74 }
75 else
76 {
77 p.pSex = "女";
78 }
79 if(personOperate.updatePerson(p))
80 {
81 Response.Write("修改用户资料成功!");
82 }
83 else
84 {
85 Response.Write("更新失败!!");
86 }
87 //修改好后重新绑定 GridView
88 this.fillDg();
89 }
90 protected void btnDel_Click(object sender, EventArgs e)
91 {
92 person p = new person();
93 p.pID = Convert.ToInt32(this.txtID.Text);
94
95 if (personOperate.delPerson(p))
96 {
97 Response.Write("删除成功!");
98 }
99 else
100 {
101 Response.Write("删除失败!!");
102 }
103 //修改好后重新绑定 GridView
104 this.fillDg();
105 }
106}
107
实体类person.cs:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11/**//// <summary>
12/// person 的摘要说明
13/// </summary>
14public class person
15{
16 public int pID;
17 public string pName;
18 public string pSex;
19
20 public person()
21 {
22 //
23 // TODO: 在此处添加构造函数逻辑
24 //
25 }
26
27}
28
操作类 personOperate.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11/**//// <summary>
12/// personOperate 的摘要说明
13/// </summary>
14public class personOperate
15{
16 public personOperate()
17 {
18 //
19 // TODO: 在此处添加构造函数逻辑
20 //
21 }
22
23
24 //链接数据库
25 public static SqlConnection createCon()
26 {
27 return new SqlConnection("server=localhost;database=login;uid=sa;pwd=admin");
28 }
29
30
31 //查找该用户是否已经存在
32 public static bool searchPerson(int pID)
33 {
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 {
40 return true; //找到该用户,该用户已经存在;
41 }
42 else
43 {
44 return false; //找不到该用户,可以添加
45 }
46 }
47
48 //GridView,呈现数据库中所有的用户
49 public static DataTable selectAllUser() //注意类型是 DataTable
50 {
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
59
60
61 //添加用户
62 public static bool AddPerson(person p)//注意传入参数
63 {
64 try
65 {
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 {
74 return false;
75 }
76 }
77
78 //修改数据
79 public static bool updatePerson(person p)
80 {
81 try
82 {
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 {
91 return false;
92 }
93 }
94
95 //删除用户资料
96 public static bool delPerson(person p)
97 {
98 try
99 {
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 {
108 return false;
109 }
110 }
111
112 //查询用户资料
113 public static selectPerson(person p)
114 {
115 try
116 {
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 {
124
125 }
126 else
127 {
128
129 }
130 }
131 catch(Exception e)
132 {
133 return false;
134 }
135 }
136}
137
昨天晚上一直搞的很郁闷,不知道在哪个地方出错,一直找不出问题所在
今天终于解决问题了,HOHO~~