color: #000033; font-family: 华文行楷\">登录通讯录
用户名: ErrorMessage=\"用户名、\" Style=\"z-index: 103; left: 372px; position: absolute; top: 229px\"Text=\"*\">
top: 221px\" Width=\"141px\" OnTextChanged=\"TextBox1_TextChanged\">
密 码:
top: 265px\">
text=\"*\">
Text=\"注册\" OnClick =\"Button2_Click\" Font-Size=\"Medium\" Height=\"26px\" Width=\"50px\" CausesValidation=\"False\" />
(2)Default.aspx.cs:
using System;
using System.Data;
using System.Configuration; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void btOK_Click(object sender, EventArgs e) {
string myConnStr = \"Data Source=.;Initial Catalog=contact;Integrated Security=True\"; SqlConnection myConn = new SqlConnection(myConnStr);
string queryStr = \"select count(*) from Login where user_id='\" + TextBox1.Text + \"'and user_pwd='\" + TextBox2.Text + \"'\"; myConn.Open();
SqlCommand cmd=new SqlCommand (queryStr, myConn);
int count=Convert.ToInt32(cmd.ExecuteScalar()); myConn.Close(); try {
if (count>0) {
Session[\"user_id\"] = TextBox1.Text; Session[\"user_pwd\"] = TextBox2.Text; Response.BufferOutput = true; Response.Redirect(\"Group.aspx\"); } else {
Label1.Text = \"用户名或密码不正确\"; } }
catch(Exception ex) {
Console.WriteLine(\"Exception in main:\" + ex.Message);
} }
protected void Button2_Click(object sender, EventArgs e) {
Response.Redirect(\"zhuce.aspx\"); }
protected void TextBox1_TextChanged(object sender, EventArgs e) {
} }
2、注册界面:zhuce: (1)Zhuce.aspx
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"zhuce\" %>
CodeFile=\"zhuce.aspx.cs\"
1.0 Transitional//EN\"
注册 (2)Zhuce.aspx.cs
using System;
using System.Data;
using System.Configuration; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class zhuce : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { }
protected void btOK_Click(object sender, EventArgs e) {
string myConnStr = \"Data Source=.;Initial Catalog=contact;Integrated Security=True\"; SqlConnection conn = new SqlConnection(myConnStr);
string queryStr = \"insert into Login(user_id,user_pwd,user_ques,user_anss) values('\" + TextBox1.Text + \"','\" + TextBox2.Text + \"','\" + TextBox5.Text + \"','\" + TextBox6.Text + \"')\";
SqlCommand myCom = new SqlCommand(queryStr, conn); if (Page.IsValid == true) {
myCom.Connection.Open(); myCom.ExecuteNonQuery(); myCom.Connection.Close(); Response.Write(\"\"); } else {
TextBox1.Text = \"\"; TextBox2.Text = \"\";
TextBox5.Text = \"\"; TextBox6.Text = \"\"; }
}
protected void Button2_Click(object sender, EventArgs e) {
Response.Redirect(\"Default.aspx\"); }
protected void Button3_Click(object sender, EventArgs e) {
TextBox1.Text = \"\"; TextBox2.Text = \"\"; TextBox3.Text = \"\";
TextBox5.Text = \"\"; TextBox6.Text = \"\"; } }
3、主界面:Group
(1) Group.aspx
册成功!<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"Group.aspx.cs\" Inherits=\"Group\" %>
Group (2)Group.aspx.cs
using System;
using System.Data;
using System.Configuration; using System.Collections; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class Group : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
this.Label1.Text = Convert.ToString(Session[\"user_id\"]); } }
4、添加新通讯录分组:AddGroup. (1)AddGroup.aspx
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"AddGroup.aspx.cs\" Inherits=\"_Default\" %>
无标题页 (2)AddGroup.aspx.cs
using System;
using System.Data;
using System.Configuration; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (Session[\"user_id\"] == null) {
Response.Write(\"\"); } else {
this.Label1.Text = this.Label1.Text = Convert.ToString(Session[\"user_id\"]); } }
protected void Button1_Click(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection(\"Data Source=.;Initial Catalog=contact;Integrated Security=True\"); conn.Open();
string sql = \"insert into TableGroup(user_id,Gname,GDescription)Values('\" + Label1.Text + \"','\" + TextBox3.Text + \"','\" + TextBox4.Text + \"')\"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close();
Response.Write(\"\"); }
protected void Button2_Click(object sender, EventArgs e) {
TextBox3.Text = \"\"; TextBox4.Text = \"\"; } }
5、添加新的联系人:AltPerson
(1)AltPersom.aspx
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"AltPerson.aspx.cs\" Inherits=\"Add\" %>
无标题页 (2)AltPersom.aspx.cs
using System;
using System.Data;
using System.Configuration; using System.Collections; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class Add : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
Bind(); }
this.Label1.Text = Convert.ToString(Session[\"user_id\"]); this.Label2.Text = Convert.ToString(Session[\"GID\"]); }
public void Bind()
{
SqlConnection conn = new SqlConnection(\"Data Source=.;Initial Catalog=contact;Integrated Security=True\");
string sql = \"select * from TableUser where GID ='\" + Session[\"GID\"] + \"'\"; SqlDataAdapter DA = new SqlDataAdapter(sql, conn); DataSet DS = new DataSet(); DA.Fill(DS);
GridView1.DataKeyNames = new string[] { \"UID\" }; GridView1.DataSource = DS.Tables[0]; GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
GridView1.EditIndex = e.NewEditIndex; Bind(); }
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
GridView1.EditIndex = -1; Bind(); }
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
int UID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
string Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text; string Gender = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text; string Address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text; string PostCode = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text; string Telephone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text;
string Mobile = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text; string Email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text;
SqlConnection conn = new SqlConnection(\"Data Source=.;Initial Catalog=contact;Integrated Security=True\");
string SqlStr = \"update TableUser set Name='\" + Name + \"',Gender='\" + Gender + \"',PostCode='\" + PostCode + \"',Telephone='\" + Telephone + \"',Address='\" + Address + \"',Mobile='\" + Mobile + \"',Email='\" + Email + \"' where UID='\" + UID + \"'\";
try {
if (conn.State.ToString() == \"Closed\") conn.Open(); SqlCommand comm = new SqlCommand(SqlStr, conn); comm.ExecuteNonQuery(); comm.Dispose();
if (conn.State.ToString() == \"Open\") conn.Close();
GridView1.EditIndex = -1; Bind(); }
catch (Exception ex) {
Response.Write(\"数据库错误,错误原因:\" + ex.Message); Response.End(); } }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
SqlConnection conn = new SqlConnection(\"Data Source=.;Initial Catalog=contact;Integrated Security=True\"); conn.Open();
string strsql = \"delete from TableUser where UID='\" + GridView1.DataKeys[e.RowIndex].Value.ToString() + \"'\";
SqlCommand comm = new SqlCommand(strsql, conn);
comm.ExecuteNonQuery(); conn.Close(); Bind(); }
protected void Button1_Click(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection(\"Data Source=.;Initial Catalog=contact;Integrated Security=True\"); conn.Open(); string sql = \"insert into TableUser(GID,Name,Gender,Address,PostCode,Telephone,Mobile,Email) values('\" + Session[\"GID\"] + \"','\" + TextBox2.Text + \"','\" + TextBox3.Text + \"','\" + TextBox4.Text + \"','\" + TextBox5.Text + \"','\" + TextBox6.Text + \"','\" + TextBox7.Text + \"','\" + TextBox8.Text + \"')\"; SqlCommand comm = new SqlCommand(sql, conn); comm.ExecuteNonQuery(); conn.Close();
Response.Write(\"\"); Bind(); }
protected void Button2_Click(object sender, EventArgs e) {
TextBox8.Text = \"\"; TextBox7.Text = \"\"; TextBox6.Text = \"\"; TextBox5.Text = \"\"; TextBox4.Text = \"\"; TextBox3.Text = \"\"; TextBox2.Text = \"\"; } }
6、修改分组:AltGroup
(1)AltGroup.aspx
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"AddGroup.aspx.cs\" Inherits=\"_Default\" %>
无标题页 (2)AltGroup.aspx.cs
using System;
using System.Data;
using System.Configuration; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (Session[\"user_id\"] == null) {
Response.Write(\"\"); } else {
this.Label1.Text = this.Label1.Text = Convert.ToString(Session[\"user_id\"]); } }
protected void Button1_Click(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection(\"Data Source=.;Initial Catalog=contact;Integrated Security=True\"); conn.Open();
string sql = \"insert into TableGroup(user_id,Gname,GDescription)Values('\" + Label1.Text + \"','\" + TextBox3.Text + \"','\" + TextBox4.Text + \"')\"; SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery(); conn.Close();
Response.Write(\"\"); }
protected void Button2_Click(object sender, EventArgs e) {
TextBox3.Text = \"\"; TextBox4.Text = \"\"; } }
四、运行程序,验证结果
1、登录
2.注册
3、添加分组
4、添加联系人
5.、查询联系人
五、设计总结
经过一个多星期的大作业设计,使我对C#有了更进一步的认识和了解,要学好它重在实践,要通过不断地上机操作才能更好地学习它。上课时学到的关于C#的东西比较少,都是一些枯燥的东西。通过大作业设计,自己动手、自己发现和解决问题,发现了许多自己的不足。平时没有掌握好的知识在设计中彻底暴露了出来,,比如与数据库连接时连接不上等等。
这次试验我视线了实验要求上的部分功能,例如通讯录的注册,创建通讯组,添加联系人,显示联系人信息与维护以及查找通讯录。但是没有实现密码修改以及编辑修改联系人的信息。
要熟悉这门语言,仅在上课时学习的那点东西是不够的,这门语言也要求我们有比较强的实践能力,因为我们学习的知识就是为了实践,而只有多实践,多编写程序,才能更好的理解与掌握课堂上所学到的东西。