更新多个表的DataGrid

时间:2013-08-16 20:30:49

标签: c# asp.net datagrid

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您的存储过程可以有多个更新/插入语句,因此一旦捕获了参数,您就可以将它们传递给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