<%@ 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>
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) { } }
<%@ 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>
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(); } }
ASP.Net增删查改写的一个通讯录系统---ShinePans
原文地址:http://blog.csdn.net/shinepan/article/details/46558615