归园

崇拜自己,相信自己,超越自己

用户操作
[即时聊天] [发私信] [加为好友]
颜春捷ID:love3e4520
16753次访问,排名7084好友2人,关注者16
为人诚恳,乐观开朗,富拼搏精神;工作积极主动,认真踏实,有强烈的责任心和团队精神;有较强的学习和适应新环境的能力,求知欲望强烈;进取心强,爱好篮球,喜欢交际。
love3e4520的文章
原创 36 篇
翻译 0 篇
转载 47 篇
评论 9 篇
况天佑的公告
鼠目不是寸光,好运非你莫鼠!
最近评论
zcw:我有很多,有意者联系277265779
agenippe:哎..
无名:我有以上硬币无数,如57年的5分,79年的啊.如有意者请面谈.QQ373513074
教科书:我有100个,买者请跟我联系,QQ44789540
教科书:fc
文章分类
收藏
    相册
    动漫人物
    踏浪有痕
    友情链接
    runtime' blog
    朋友的博客
    老师的博客
    存档
    订阅我的博客
    XML聚合  FeedSky

    原创 数据库操作---插入、修改、删除收藏

    新一篇: 硬币收藏价值 | 旧一篇: .NET内置对象之Cache对象

     新建一个网站,一个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);
        }
    }

    发表于 @ 2007年11月07日 10:02:00|评论(loading...)|编辑

    新一篇: 硬币收藏价值 | 旧一篇: .NET内置对象之Cache对象

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 况天佑