码迷,mamicode.com
首页 > Web开发 > 详细

ASP.Net增删查改写的一个通讯录系统---ShinePans

时间:2015-06-19 11:51:16      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:.net   asp.net   sqlserver   c#   

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();
    }
}




其他的 改 ,删 等原理相同,参考以上代码即可.


ASP.Net增删查改写的一个通讯录系统---ShinePans

标签:.net   asp.net   sqlserver   c#   

原文地址:http://blog.csdn.net/shinepan/article/details/46558615

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!