1.开发环境
vs 2013,sqlserver 2012,win 8.1x64,C#语言,.net 4.5
2.更能说明
通讯录人性化的增删查改
3.网页效果
1.主页:
2. 查看全部联系人
3.查找职业为学生的联系人:
4.查找指定姓名的联系人:
5.添加联系人:
6.添加后:
7.更新联系人:
8.更新前:
9.更新后:
10.删除联系人:
11.删除前:
12.删除后:
13.关于:
4.关键代码:
default.aspx:
<%@ Page Title="Delete" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Delete.aspx.cs" Inherits="Delete" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">查看所有的联系人</asp:LinkButton>
</p>
<p>
所有联系人如下,请输入要删除的人的姓名或编号或电话号码:</p>
<p>
按电话号码删除:</p>
<p>
<asp:TextBox ID="Phone" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除" />
</p>
<p>
按名字删除:</p>
<p>
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="删除" />
</p>
<p>
按编号删除:</p>
<p>
<asp:TextBox ID="ID" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="删除" />
</p>
<p>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" Width="739px">
</asp:GridView>
</p>
</asp:Content>
default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string ConString="Data Source=(LocalDB)\\v11.0;AttachDbFilename=d:\\UserFile\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\App_Data\\contact.mdf;Integrated Security=True";
string sql1 = "select * from contact";
SqlConnection myconn = new SqlConnection(ConString);
SqlCommand mycmd = new SqlCommand(sql1, myconn);
SqlDataAdapter sda = new SqlDataAdapter(mycmd);
DataSet ds = new DataSet();
sda.Fill(ds, "contact");
this.GridView1.DataSource = ds.Tables["contact"];
this.GridView1.DataBind();
myconn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string ConString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=d:\\UserFile\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\App_Data\\contact.mdf;Integrated Security=True";
string sql1 = "select * from contact where name='"+this.Name.Text.ToString().Trim()+"';";
SqlConnection myconn = new SqlConnection(ConString);
SqlCommand mycmd = new SqlCommand(sql1, myconn);
SqlDataAdapter sda = new SqlDataAdapter(mycmd);
DataSet ds = new DataSet();
sda.Fill(ds, "contact");
this.GridView1.DataSource = ds.Tables["contact"];
this.GridView1.DataBind();
myconn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string ConString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=d:\\UserFile\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\App_Data\\contact.mdf;Integrated Security=True";
string sql1 = "select * from contact where address='"+this.Address.Text+"';";
SqlConnection myconn = new SqlConnection(ConString);
SqlCommand mycmd = new SqlCommand(sql1, myconn);
SqlDataAdapter sda = new SqlDataAdapter(mycmd);
DataSet ds = new DataSet();
sda.Fill(ds, "contact");
this.GridView1.DataSource = ds.Tables["contact"];
this.GridView1.DataBind();
myconn.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
string ConString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=d:\\UserFile\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\App_Data\\contact.mdf;Integrated Security=True";
string sql1 = "select * from contact where vocation='"+this.vocation.Text+"';";
SqlConnection myconn = new SqlConnection(ConString);
SqlCommand mycmd = new SqlCommand(sql1, myconn);
SqlDataAdapter sda = new SqlDataAdapter(mycmd);
DataSet ds = new DataSet();
sda.Fill(ds, "contact");
this.GridView1.DataSource = ds.Tables["contact"];
this.GridView1.DataBind();
myconn.Close();
}
protected void vocation_TextChanged(object sender, EventArgs e)
{
}
}
add.aspx:
<%@ Page Title="Add" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Add.aspx.cs" Inherits="Add" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>添加联系人</h1>
<p class="lead"> Add new information...</p>
<p class="lead">ID:
<asp:TextBox ID="ID" runat="server"></asp:TextBox>
</p>
<p class="lead">姓名:<asp:TextBox ID="Name" runat="server"></asp:TextBox>
</p>
<p class="lead">性别:<asp:TextBox ID="Sex" runat="server"></asp:TextBox>
</p>
<p class="lead">年龄:<asp:TextBox ID="Age" runat="server"></asp:TextBox>
</p>
<p class="lead">住址:<asp:TextBox ID="Address" runat="server"></asp:TextBox>
</p>
<p class="lead">职业:<asp:TextBox ID="Vacation" runat="server"></asp:TextBox>
</p>
<p class="lead">电话:<asp:TextBox ID="Phone" runat="server"></asp:TextBox>
</p>
<p class="lead">
<asp:Button ID="Submit" runat="server" OnClick="Submit_Click" Text="提交" />
</p>
<p class="lead">当前已存在的联系人:</p>
<p class="lead"> </p>
</div>
<div class="row">
<asp:GridView ID="GridView1" runat="server" Height="151px" Width="794px">
</asp:GridView>
<br />
<br />
</div>
</asp:Content>
add.aspx.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string tempName = this.Name.Text.Trim();
string ConString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=d:\\UserFile\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\App_Data\\contact.mdf;Integrated Security=True";
string sql1 = "select * from contact";
SqlConnection myconn = new SqlConnection(ConString);
SqlCommand mycmd = new SqlCommand(sql1, myconn);
SqlDataAdapter sda = new SqlDataAdapter(mycmd);
DataSet ds = new DataSet();
sda.Fill(ds, "contact");
this.GridView1.DataSource = ds.Tables["contact"];
this.GridView1.DataBind();
myconn.Close();
}
protected void Submit_Click(object sender, EventArgs e)
{
string tempName=this.Name.Text.Trim();
string ConString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=d:\\UserFile\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\App_Data\\contact.mdf;Integrated Security=True";
string sql1 = "insert into contact values(@ID,@name,@address,@phonenumber,@age,@sex,@vocation)";
SqlConnection myconn = new SqlConnection(ConString);
SqlCommand mycmd = new SqlCommand(sql1, myconn);
mycmd.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int));
mycmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 50));
mycmd.Parameters.Add(new SqlParameter("@address", SqlDbType.NVarChar, 50));
mycmd.Parameters.Add(new SqlParameter("@phonenumber", SqlDbType.NVarChar, 50));
mycmd.Parameters.Add(new SqlParameter("@age", SqlDbType.Int));
mycmd.Parameters.Add(new SqlParameter("@sex", SqlDbType.NChar, 10));
mycmd.Parameters.Add(new SqlParameter("@vocation", SqlDbType.NVarChar, 50));
mycmd.Parameters["@ID"].Value = this.ID.Text;
mycmd.Parameters["@name"].Value = this.Name.Text;
mycmd.Parameters["@address"].Value = this.Address.Text;
mycmd.Parameters["@phonenumber"].Value = this.Phone.Text;
mycmd.Parameters["@age"].Value = this.Age.Text;
mycmd.Parameters["@sex"].Value = this.Sex.Text;
mycmd.Parameters["@vocation"].Value = this.Vacation.Text;
myconn.Open();
mycmd.ExecuteNonQuery();
//Reset all textbox
this.ID.Text = "";
this.Name.Text = "";
this.Address.Text = "";
this.Phone.Text = "";
this.Age.Text = "";
this.Sex.Text = "";
this.Vacation.Text = "";
Response.Write("<script>alert(‘已近添加成功,请见添加成功的数据')</script>");
string sql2 = "select * from contact";
SqlCommand newcmd = new SqlCommand(sql2, myconn);
SqlDataAdapter sda = new SqlDataAdapter(newcmd);
DataSet ds = new DataSet();
sda.Fill(ds, "contact");
this.GridView1.DataSource = ds.Tables["contact"];
this.GridView1.DataBind();
myconn.Close();
}
}
其他的 改 ,删 等原理相同,参考以上代码即可.