asp.net更新指定记录实现程序

作者:袖梨 2022-06-25

我们先来看html页面

 代码如下 复制代码

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

 


         
       

           
               
                   
               
               
                   
               
               
                   
               
               
                   
               
               
                   
               
               
                   
               
               
                   
               
           

                       

       
           
               
               
               
               
                                    HeaderText="更新" Text="更新" />
           

           
           
           
           
           
           
           
       

                   

                         

                       
                   

                       
       

                       
       

                         

       

   

由上面页面提交过来的数据我们接受然后利用sql执行更新数据库

 代码如下 复制代码

View Code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)//首次执行页面时
        {
            GridViewBind();//绑定自定义方法GridViewBind
            if (Request.QueryString["商品编号"] != null)//判断,如果可以获取到id的值,则执行以下操作
            {
                SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
                con.Open();
                SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05 where 商品编号=" + Request.QueryString["商品编号"] + "", con);
                DataSet ds = new DataSet();
                ada.Fill(ds, "tb_shopping05");
                DataRowView drv = ds.Tables["tb_shopping05"].DefaultView[0];
                this.TxtName.Text = drv["商品名称"].ToString();
                this.TxtNum.Text = drv["商品数量"].ToString();
                this.TxtPrice.Text = drv["商品单价"].ToString();
            }
        }
    }
    public void GridViewBind()//绑定GridView控件的自定义方法
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        con.Open();
        SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05", con);
        DataSet ds = new DataSet();
        ada.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
            con.Open();
            SqlCommand com = new SqlCommand("update tb_shopping05 set 商品名称='" + this.TxtName.Text + "',商品数量='" + this.TxtNum.Text + "',商品单价='" + this.TxtPrice.Text + "' where 商品编号=" + Request["商品编号"], con);
            com.ExecuteNonQuery();
            GridViewBind();
            Response.Write("");
        }
        catch
        {
            Response.Write("");
        }
    }
}

原理是这样的,我们点击经编辑的数据时会传一个ID过来,然后我们再利用sql把接受过来的数据进行update即可了。

相关文章

精彩推荐