1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGrid.aspx.cs" Inherits="Ado_DataGrid" %>
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 style="z-index: 101; left: 199px; 728px; position: absolute; top: 53px;
13 height: 344px">
14 <table border="1" cellpadding="1">
15 <tr>
16 <td style=" 79px; background-color: #3399ff; height: 28px;">
17 编号:</td>
18 <td style=" 174px; background-color: #3399ff; height: 28px;">
19 <asp:TextBox ID="txtID" runat="server"></asp:TextBox></td>
20 <td style=" 222px; background-color: #3399ff; height: 28px;">
21 <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtID"
22 Display="Dynamic" ErrorMessage="该用户已经存在" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator></td>
23 </tr>
24 <tr>
25 <td style=" 79px; height: 23px; background-color: #3399ff">
26 姓名:</td>
27 <td style=" 174px; height: 23px; background-color: #3399ff">
28 <asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
29 <td style=" 222px; height: 23px; background-color: #3399ff">
30 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
31 ErrorMessage="*"></asp:RequiredFieldValidator></td>
32 </tr>
33 <tr>
34 <td style=" 79px; height: 24px; background-color: #3399ff">
35 性别:</td>
36 <td style=" 174px; height: 24px; background-color: #3399ff">
37 <asp:RadioButton ID="sexNan" runat="server" GroupName="sex" Text="男" Checked="True" />
38 <asp:RadioButton ID="sexNv" runat="server" GroupName="sex" Text="女" />
39 </td>
40 <td style=" 222px; height: 24px; background-color: #3399ff">
41 </td>
42 </tr>
43 <tr>
44 <td style="height: 22px; background-color: #3399ff; text-align: center;" colspan="3">
45 <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
46 <asp:Button ID="btnupdate" runat="server" OnClick="btnupdate_Click" Text="修改" />
47 <asp:Button ID="btnDel" runat="server" Text="删除" />
48 <asp:Button ID="btnsel" runat="server" Text="查询" /></td>
49 </tr>
50 </table>
51 <br />
52 <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966"
53 BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="498px">
54 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
55 <RowStyle BackColor="White" ForeColor="#330099" />
56 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
57 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
58 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
59 </asp:GridView>
60 </div>
61
62 </div>
63 </form>
64</body>
65</html>
66
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 style="z-index: 101; left: 199px; 728px; position: absolute; top: 53px;
13 height: 344px">
14 <table border="1" cellpadding="1">
15 <tr>
16 <td style=" 79px; background-color: #3399ff; height: 28px;">
17 编号:</td>
18 <td style=" 174px; background-color: #3399ff; height: 28px;">
19 <asp:TextBox ID="txtID" runat="server"></asp:TextBox></td>
20 <td style=" 222px; background-color: #3399ff; height: 28px;">
21 <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtID"
22 Display="Dynamic" ErrorMessage="该用户已经存在" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator></td>
23 </tr>
24 <tr>
25 <td style=" 79px; height: 23px; background-color: #3399ff">
26 姓名:</td>
27 <td style=" 174px; height: 23px; background-color: #3399ff">
28 <asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
29 <td style=" 222px; height: 23px; background-color: #3399ff">
30 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
31 ErrorMessage="*"></asp:RequiredFieldValidator></td>
32 </tr>
33 <tr>
34 <td style=" 79px; height: 24px; background-color: #3399ff">
35 性别:</td>
36 <td style=" 174px; height: 24px; background-color: #3399ff">
37 <asp:RadioButton ID="sexNan" runat="server" GroupName="sex" Text="男" Checked="True" />
38 <asp:RadioButton ID="sexNv" runat="server" GroupName="sex" Text="女" />
39 </td>
40 <td style=" 222px; height: 24px; background-color: #3399ff">
41 </td>
42 </tr>
43 <tr>
44 <td style="height: 22px; background-color: #3399ff; text-align: center;" colspan="3">
45 <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
46 <asp:Button ID="btnupdate" runat="server" OnClick="btnupdate_Click" Text="修改" />
47 <asp:Button ID="btnDel" runat="server" Text="删除" />
48 <asp:Button ID="btnsel" runat="server" Text="查询" /></td>
49 </tr>
50 </table>
51 <br />
52 <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966"
53 BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="498px">
54 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
55 <RowStyle BackColor="White" ForeColor="#330099" />
56 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
57 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
58 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
59 </asp:GridView>
60 </div>
61
62 </div>
63 </form>
64</body>
65</html>
66
后台:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Ado_DataGrid : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 this.fillDg();
17 }
18 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
19 {
20 string pID = args.Value;
21 if (personOprate.findPerson(pID))
22 {
23 args.IsValid = false; //该用户已经存在,验证不能通过
24 }
25 else
26 {
27 args.IsValid = true; //该用户可以添加
28 }
29 }
30
31 private void fillDg()
32 {
33 this.GridView1.DataSource = personOprate.selectAllperson();
34 this.GridView1.DataBind();
35 }
36
37 protected void btnAdd_Click(object sender, EventArgs e)
38 {
39 if(this.IsValid)
40 {
41 person p = new person();
42 p.pID = this.txtID.Text;
43 p.pName = this.txtName.Text;
44 if(this.sexNan.Checked)
45 {
46 p.pSex = "男";
47 }
48 else
49 {
50 p.pSex = "女";
51 }
52 if (personOprate.insertOprate(p))
53 {
54 Response.Write("插入成功!");
55 this.fillDg();
56 }
57 else
58 {
59 Response.Write("<font color=red>插入失败!</font>");
60 }
61 }
62 }
63 protected void btnupdate_Click(object sender, EventArgs e)
64 {
65 //如果用户存在,则可以修改
66 if (!this.CustomValidator1.IsValid)
67 {
68 person p = new person();
69 p.pID = this.txtID.Text;
70 p.pName = this.txtName.Text;
71 if(this.sexNan.Checked)
72 {
73 p.pSex = "男";
74 }
75 else
76 {
77 p.pSex = "女";
78 }
79
80 if (personOprate.updateUser(p))
81 {
82 Response.Write("修改成功!");
83 }
84 else
85 {
86 Response.Write("修改出错!");
87 }
88 this.fillDg();
89 }
90 else
91 {
92 Response.Write("<font color=red>用户不存在,修改失败!</font>");
93 }
94 }
95}
96
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Ado_DataGrid : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 this.fillDg();
17 }
18 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
19 {
20 string pID = args.Value;
21 if (personOprate.findPerson(pID))
22 {
23 args.IsValid = false; //该用户已经存在,验证不能通过
24 }
25 else
26 {
27 args.IsValid = true; //该用户可以添加
28 }
29 }
30
31 private void fillDg()
32 {
33 this.GridView1.DataSource = personOprate.selectAllperson();
34 this.GridView1.DataBind();
35 }
36
37 protected void btnAdd_Click(object sender, EventArgs e)
38 {
39 if(this.IsValid)
40 {
41 person p = new person();
42 p.pID = this.txtID.Text;
43 p.pName = this.txtName.Text;
44 if(this.sexNan.Checked)
45 {
46 p.pSex = "男";
47 }
48 else
49 {
50 p.pSex = "女";
51 }
52 if (personOprate.insertOprate(p))
53 {
54 Response.Write("插入成功!");
55 this.fillDg();
56 }
57 else
58 {
59 Response.Write("<font color=red>插入失败!</font>");
60 }
61 }
62 }
63 protected void btnupdate_Click(object sender, EventArgs e)
64 {
65 //如果用户存在,则可以修改
66 if (!this.CustomValidator1.IsValid)
67 {
68 person p = new person();
69 p.pID = this.txtID.Text;
70 p.pName = this.txtName.Text;
71 if(this.sexNan.Checked)
72 {
73 p.pSex = "男";
74 }
75 else
76 {
77 p.pSex = "女";
78 }
79
80 if (personOprate.updateUser(p))
81 {
82 Response.Write("修改成功!");
83 }
84 else
85 {
86 Response.Write("修改出错!");
87 }
88 this.fillDg();
89 }
90 else
91 {
92 Response.Write("<font color=red>用户不存在,修改失败!</font>");
93 }
94 }
95}
96
实体类 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/// 这是一个实体类,传参.
14/// </summary>
15public class person
16{
17 public string pID;
18 public string pName;
19 public string pSex;
20
21 public person()
22 {
23 //
24 // TODO: 在此处添加构造函数逻辑
25 //
26 }
27}
28
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/// 这是一个实体类,传参.
14/// </summary>
15public class person
16{
17 public string pID;
18 public string pName;
19 public string pSex;
20
21 public person()
22 {
23 //
24 // TODO: 在此处添加构造函数逻辑
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;
11using System.Data.SqlClient;
12/// <summary>
13/// personOprate 的摘要说明
14/// 操作类
15/// </summary>
16public class personOprate
17{
18 public personOprate()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24 public static SqlConnection createCon()
25 {
26 return new SqlConnection("server=localhost;database=book;uid=sa;pwd=admin");
27 }
28
29
30 public static bool findPerson(string pID)
31 {
32 SqlConnection con = personOprate.createCon();
33 con.Open();
34 SqlCommand cmd = new SqlCommand("select pid from person where pid='" + pID +"'",con);
35 int count = Convert.ToInt32(cmd.ExecuteScalar());
36 if (count > 0)
37 {
38 return true; //该用户已经存在
39 }
40 else
41 {
42 return false; //该用户不存在,可以添加
43 }
44 }
45
46 public static DataTable selectAllperson()
47 {
48 SqlConnection con = personOprate.createCon();
49 SqlDataAdapter sda = new SqlDataAdapter();
50 sda.SelectCommand = new SqlCommand("select * from person",con);
51 DataSet ds = new DataSet();
52 sda.Fill(ds,"person"); //填充数据集.填充完后,数据已经从数据库到达本地了.接下来只要返回就可以了.
53 return ds.Tables["person"];
54
55 }
56
57 public static bool insertOprate(person p)
58 {
59 //使用参数
60 try //只要 try 里面任何一句出错,则返回 false;
61 {
62 SqlConnection con = personOprate.createCon();
63 con.Open();
64 SqlCommand cmd = new SqlCommand("insert into person values(@pID,@pName,@pSex)", con);
65
66 SqlParameter para = new SqlParameter("@pID", SqlDbType.VarChar, 10);
67 para.Value = p.pID;
68 cmd.Parameters.Add(para);
69
70 SqlParameter para2 = new SqlParameter("pName", SqlDbType.VarChar, 20);
71 para2.Value = p.pName;
72 cmd.Parameters.Add(para2);
73
74 SqlParameter para3 = new SqlParameter("pSex", SqlDbType.VarChar, 2);
75 para3.Value = p.pSex;
76 cmd.Parameters.Add(para3);
77
78 cmd.ExecuteNonQuery();
79 return true;
80 }
81 catch(Exception e) //注意 Exception e
82 {
83 return false;
84 }
85 }
86
87
88 //修改用户信息
89 public static bool updateUser(person p)
90 {
91 try
92 {
93 //SqlConnection con = personOprate.createCon();
94 //con.Open();
95 //SqlCommand cmd = new SqlCommand("update person set personName='" + p.pName + "',personSex='" + p.pSex + "' where personID='" + p.pID + "'", con);
96 //cmd.ExecuteNonQuery();
97 //return true;
98
99 SqlConnection con = personOprate.createCon();
100 con.Open();
101 SqlCommand cmd = new SqlCommand("update person set personName='wgf',personSex='女' where personID='"+p.pID+"'",con);
102 cmd.ExecuteNonQuery();
103 return true;
104
105 //SqlConnection con = personOprate.createCon();
106 //con.Open();
107 //SqlCommand cmd = new SqlCommand("update person set (personName,personSex) values(@pName,@pSex)",con);
108
109 //SqlParameter para2 = new SqlParameter("pName", SqlDbType.VarChar, 20);
110 //para2.Value = p.pName;
111 //cmd.Parameters.Add(para2);
112
113 //SqlParameter para3 = new SqlParameter("pSex", SqlDbType.VarChar, 2);
114 //para3.Value = p.pSex;
115 //cmd.Parameters.Add(para3);
116
117 //cmd.ExecuteNonQuery();
118 //return true;
119 }
120 catch(Exception e) //Exception e 是什么意思呢?不明白
121 {
122 return false;
123 }
124 }
125
126}
127
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;
11using System.Data.SqlClient;
12/// <summary>
13/// personOprate 的摘要说明
14/// 操作类
15/// </summary>
16public class personOprate
17{
18 public personOprate()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24 public static SqlConnection createCon()
25 {
26 return new SqlConnection("server=localhost;database=book;uid=sa;pwd=admin");
27 }
28
29
30 public static bool findPerson(string pID)
31 {
32 SqlConnection con = personOprate.createCon();
33 con.Open();
34 SqlCommand cmd = new SqlCommand("select pid from person where pid='" + pID +"'",con);
35 int count = Convert.ToInt32(cmd.ExecuteScalar());
36 if (count > 0)
37 {
38 return true; //该用户已经存在
39 }
40 else
41 {
42 return false; //该用户不存在,可以添加
43 }
44 }
45
46 public static DataTable selectAllperson()
47 {
48 SqlConnection con = personOprate.createCon();
49 SqlDataAdapter sda = new SqlDataAdapter();
50 sda.SelectCommand = new SqlCommand("select * from person",con);
51 DataSet ds = new DataSet();
52 sda.Fill(ds,"person"); //填充数据集.填充完后,数据已经从数据库到达本地了.接下来只要返回就可以了.
53 return ds.Tables["person"];
54
55 }
56
57 public static bool insertOprate(person p)
58 {
59 //使用参数
60 try //只要 try 里面任何一句出错,则返回 false;
61 {
62 SqlConnection con = personOprate.createCon();
63 con.Open();
64 SqlCommand cmd = new SqlCommand("insert into person values(@pID,@pName,@pSex)", con);
65
66 SqlParameter para = new SqlParameter("@pID", SqlDbType.VarChar, 10);
67 para.Value = p.pID;
68 cmd.Parameters.Add(para);
69
70 SqlParameter para2 = new SqlParameter("pName", SqlDbType.VarChar, 20);
71 para2.Value = p.pName;
72 cmd.Parameters.Add(para2);
73
74 SqlParameter para3 = new SqlParameter("pSex", SqlDbType.VarChar, 2);
75 para3.Value = p.pSex;
76 cmd.Parameters.Add(para3);
77
78 cmd.ExecuteNonQuery();
79 return true;
80 }
81 catch(Exception e) //注意 Exception e
82 {
83 return false;
84 }
85 }
86
87
88 //修改用户信息
89 public static bool updateUser(person p)
90 {
91 try
92 {
93 //SqlConnection con = personOprate.createCon();
94 //con.Open();
95 //SqlCommand cmd = new SqlCommand("update person set personName='" + p.pName + "',personSex='" + p.pSex + "' where personID='" + p.pID + "'", con);
96 //cmd.ExecuteNonQuery();
97 //return true;
98
99 SqlConnection con = personOprate.createCon();
100 con.Open();
101 SqlCommand cmd = new SqlCommand("update person set personName='wgf',personSex='女' where personID='"+p.pID+"'",con);
102 cmd.ExecuteNonQuery();
103 return true;
104
105 //SqlConnection con = personOprate.createCon();
106 //con.Open();
107 //SqlCommand cmd = new SqlCommand("update person set (personName,personSex) values(@pName,@pSex)",con);
108
109 //SqlParameter para2 = new SqlParameter("pName", SqlDbType.VarChar, 20);
110 //para2.Value = p.pName;
111 //cmd.Parameters.Add(para2);
112
113 //SqlParameter para3 = new SqlParameter("pSex", SqlDbType.VarChar, 2);
114 //para3.Value = p.pSex;
115 //cmd.Parameters.Add(para3);
116
117 //cmd.ExecuteNonQuery();
118 //return true;
119 }
120 catch(Exception e) //Exception e 是什么意思呢?不明白
121 {
122 return false;
123 }
124 }
125
126}
127
表person 字段: pID,personName,personSex
这里哪里有错呢,不明白啊!!!
1 SqlConnection con = personOprate.createCon();
2 con.Open();
3 SqlCommand cmd = new SqlCommand("update person set personName='" + p.pName + "',personSex='" + p.pSex + "' where personID='" + p.pID + "'", con);
4 cmd.ExecuteNonQuery();
5 return true;
2 con.Open();
3 SqlCommand cmd = new SqlCommand("update person set personName='" + p.pName + "',personSex='" + p.pSex + "' where personID='" + p.pID + "'", con);
4 cmd.ExecuteNonQuery();
5 return true;