图书管理数据更新部份

using System;
using System.Collections;
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 Update : System.Web.UI.Page
{
    static string strconn = "Data Source=localhost;database=BOOKS;integrated security=SSPI";
    SqlConnection conn = new SqlConnection(strconn);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGridView();
        }
    }
    private void LoadGridView()
    {
        ConnectionDB();
        string SQL = "SELECT * FROM BOOKSTORAGE";
        SqlCommand cmd = conn.CreateCommand();
        SqlDataAdapter adaper = new SqlDataAdapter(SQL, strconn);
        DataSet dataset = new DataSet();
        adaper.Fill(dataset, "BOOKSTORAGE");
        CloseConnection();
        GVUpdate.DataSource = dataset.Tables["BOOKSTORAGE"];
        GVUpdate.DataBind();
    }
    protected void GVUpdate_RowEditing(object sender, GridViewEditEventArgs e)
    {
       GVUpdate.EditIndex = e.NewEditIndex;
       LoadGridView();

    }
    protected void GVUpdate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GVUpdate.EditIndex = -1;
        LoadGridView();
    }
    protected void GVUpdate_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //使用e.RowIndex获取当前变化行
        //使用GVUpdate.Rows[e.RowIndex].Cells[0].Text获取id值
         int CategoryID = Convert.ToInt32(GVUpdate.Rows[e.RowIndex].Cells[0].Text);
       
        //获取当前编辑页面中的TextBox对象,其中内容为用户新输入的数据
        TextBox Tempbk_name = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[1].Controls[0];
        TextBox Tempbk_author = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[2].Controls[0];
        TextBox Tempprice = (TextBox)GVUpdate.Rows[e.RowIndex].Cells[3].Controls[0];
            TextBox bk_content=(TextBox)GVUpdate.Rows[e.RowIndex].Cells[4].Controls[0];
 
        string bk_name = Tempbk_name.Text;
        string bk_author=Tempbk_author.Text;
        decimal price =  Convert.ToDecimal(Tempprice.Text);
        string bkcontent = bk_content.Text;
      
        GVUpdate.EditIndex = -1;//由于网格中的索引值从0开始,只需将GridView控件的EditIndex属性设置为-1,即可取消编辑

        ConnectionDB();
        string SelectSql = "SELECT * FROM BOOKSTORAGE";
        SqlDataAdapter adapter = new SqlDataAdapter(SelectSql, conn);
        DataSet dataset = new DataSet();
        adapter.Fill(dataset, "BOOKSTORAGE");
        DataTable table = dataset.Tables["BOOKSTORAGE"];
       
        //设置主鍵码
        table.PrimaryKey = new DataColumn[] { table.Columns["id"] };

        //用Find方法查找要修改的行
        DataRow Rowbk_name = table.Rows.Find(CategoryID);
        DataRow Rowbk_author = table.Rows.Find(CategoryID);
        DataRow Rowprice = table.Rows.Find(CategoryID);
        DataRow Rowcontent = table.Rows.Find(CategoryID);
      
        //进行修改数据
        Rowbk_name["bk_name"] = bk_name;
        Rowbk_author["bk_author"] = bk_author;
        Rowbk_author["price"] = price;
        Rowcontent["bk_content"] = bkcontent;

        //创建CommandBuilder对象
        SqlCommandBuilder myCB = new SqlCommandBuilder(adapter);
        SqlCommand CMD= myCB.GetDeleteCommand();
        //将修改后的数据表推入到数据庫中
        adapter.Update(dataset.Tables["BOOKSTORAGE"]);
        CloseConnection();
        LoadGridView();
    }
    private void ConnectionDB()
    {
        if (conn == null)
        {
            conn = new SqlConnection(strconn);
        }
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
    }
    private void CloseConnection()
    {
        conn.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Manager.aspx");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值