数据库操作---插入、修改、删除

 新建一个网站,一个default.aspx文件,代码如下:

Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>数据库操作</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333" GridLines="None" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID编号">
                    <ItemStyle HorizontalAlign="Left" />
                    <HeaderStyle HorizontalAlign="Left" />
                    <FooterStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="name" HeaderText="用户名">
                    <ItemStyle HorizontalAlign="Left" />
                    <HeaderStyle HorizontalAlign="Center" />
                    <FooterStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="pwd" HeaderText="密码">
                    <ItemStyle HorizontalAlign="Left" />
                    <HeaderStyle HorizontalAlign="Center" />
                    <FooterStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="datatime"  DataFormatString="{0:yyyy年MM月dd日}" HeaderText="时间" HtmlEncode="False">
                    <ItemStyle HorizontalAlign="Left" />
                    <HeaderStyle HorizontalAlign="Center" />
                    <FooterStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="Default.aspx?id={0}"
                    HeaderText="编辑" Text="编辑" >
                    <ControlStyle Font-Underline="False" />
                </asp:HyperLinkField>
                <asp:CommandField HeaderText="删除" ShowDeleteButton="True">
                    <ControlStyle Font-Underline="False" />
                </asp:CommandField>
            </Columns>
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2461BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:Label ID="Label1" runat="server" Width="324px"></asp:Label><br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="插入数据" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="修改数据" OnClick="Button2_Click" /></div>
    </form>
</body>
</html>
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)
    {
        SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("select * from tb_Info", con);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "tb_Info");
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();   
        con.Close();
        if (Page.IsPostBack == false)
        {
            if (Request.QueryString["id"] == null)
            {
                Label1.Visible = false;
            }
            else
            {
                SqlConnection mycon = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;DataBase=snear");
                mycon.Open();
                SqlDataAdapter ada = new SqlDataAdapter("select * from tb_Info where id=" + Request.QueryString["id"], mycon);
                DataSet myds = new DataSet();
                ada.Fill(myds, "tb_Info");
                DataRowView drv = myds.Tables["tb_Info"].DefaultView[0];
                TextBox1.Text = drv["name"].ToString();
                TextBox2.Text = drv["pwd"].ToString();
                Label1.Visible = true;
                Label1.Text = "您当前要修改表中的第[" + Request.QueryString["id"] + "]条记录!";
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
        con.Open();
        SqlCommand com = new SqlCommand("insert into tb_Info(name,pwd) values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
        com.ExecuteNonQuery();
        con.Close();
        Page_Load(sender, e);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "" && TextBox2.Text != "")
        {
            SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
            con.Open();
            string Sqlstr = "update tb_Info set name='" + TextBox1.Text + "',pwd='" + TextBox2.Text + "' where id=" + Request.QueryString["id"];
            SqlCommand com = new SqlCommand(Sqlstr, con);
            com.ExecuteNonQuery();
            con.Close();
            Label1.Visible = false;
            TextBox1.Text = "";
            TextBox2.Text = "";
            Page_Load(sender, e);
            Page_Load(sender, e);
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
        con.Open();
        SqlCommand com = new SqlCommand("delete from tb_Info where id='" + GridView1.DataKeys[e.RowIndex].Value + "'", con);
        com.ExecuteNonQuery();
        con.Close();
        Page_Load(sender, e);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值