更新多个表的DataGrid

本文关键字:DataGrid 更新 | 更新日期: 2023-09-27 18:28:01

我正在用多个表的结果填充一个网格视图。我的问题是如何编写存储过程来同时更新多个表。我正在捕获变量中的值,如下所示,所以现在我只需要将它们传递给sp来更新数据库和网格视图吗?

protected void gvOrderDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
        int key = Convert.ToInt32(gvOrderDetails.DataKeys[e.RowIndex].Value);
        TextBox TrackingID = (TextBox)(gvOrderDetails.Rows[e.RowIndex].FindControl("txtEditTrackingID"));
        TextBox OrderDate = (TextBox)(gvOrderDetails.Rows[e.RowIndex].FindControl("txtEditOrderDate"));
        TextBox CustID = (TextBox)(gvOrderDetails.Rows[e.RowIndex].FindControl("txtEditCustID"));
        TextBox CustomerName = (TextBox)(gvOrderDetails.Rows[e.RowIndex].FindControl("txtEditCustName"));
        TextBox Address = (TextBox)(gvOrderDetails.Rows[e.RowIndex].FindControl("txtEditAddress"));
        TextBox RouteID = (TextBox)(gvOrderDetails.Rows[e.RowIndex].FindControl("txtEditRouteID"));
        // reset to original view
        gvOrderDetails.EditIndex = -1;
        //Databind
        GrabandPostData();
    }

编辑:使用sql server 2012

更新多个表的DataGrid

您的存储过程可以有多个更新/插入语句,因此一旦您捕获了参数,您就可以将它们传递给SP,SP将根据您放入其中的逻辑进行操作,例如

CREATE PROCEDURE MyTablesUpdate(@TrackingID int, @OrderDate datetime, @CustID int, @CustomerName varchar(250)) 
AS
BEGIN
     UPDATE ORDERS SET OrderDate = @OrderDate WHERE TrackingID = @TrackingID 
     UPDATE CUSTOMERS SET CustomerName = @CustomerName WHERE CustID = @CustID
END

创建一个存储过程并将所有值作为参数传递。然后在存储过程中编写两条更新语句来更新两个不同的表。