将GridView参数传递给存储过程

时间:2013-04-02 16:24:03

标签: asp.net sql-server stored-procedures

我正在处理我之前没有处理过的一些新概念,所以我可能会使这个问题复杂化。

我有一个从5表连接填充的GridView。每列代表Join中的5个表中的一个。我想要实现的功能是:

  1. 用户选择自动生成的Edit LinkBut​​ton
  2. 用户编辑行中的一个或多个单元格
  3. 用户选择自动生成的Update LinkBut​​ton
  4. Params传递给Stored Proc,用于更新受影响的表格。
  5. 关于如何继续,我有几个不确定因素。 如果我直接查询更新,我需要从另一列传递ID,更改的文本和外键ID。 所以我的问题是如何让GridView UpdateCommand将整行的数据传递给存储的Proc? 我目前已经设置了存储过程,并使用了一个额外的参数来确定哪个表需要更新,但由于它可能是多个表,我认为这是错误的方法。这是我的GV,DSN和SP。

     <asp:GridView ID="GridView1"
        runat="server"
        AutoGenerateColumns="False" 
        DataKeyNames="id" DataSourceID="SqlDataSource1" 
        EnableModelValidation="True"
        AllowSorting="True" 
        EnableSortingAndPagingCallbacks="True"
        allowpaging="True"
        pagesize="26"
        ondatabound="GridView1_DataBound">    
    
            <Columns>
    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="True"
     ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer"   
    SortExpression="Manufacturer" />
                <asp:BoundField DataField="PriModel" HeaderText="PriModel" 
    SortExpression="PriModel" />
                <asp:BoundField DataField="SecModel" HeaderText="SecModel"  
    SortExpression="SecModel" />
                <asp:BoundField DataField="Form" HeaderText="FormFactor" 
    SortExpression="Form" />
                <asp:BoundField DataField="Active" HeaderText="Active" 
    SortExpression="Active"/>
                <asp:CommandField ShowEditButton="True" />                
            </Columns>
           </asp:GridView>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet"
        ConnectionString="<%$ ConnectionStrings:inventory_v2ConnectionString %>" 
        SelectCommand="SELECT dbo.Models.ID,dbo.Models.Active, 
        dbo.Models_PriModels.PriModel, dbo.Models_SecModels.SecModel, 
        dbo.Models_OEMs.Manufacturer, dbo.Models_FormFactor.Form
        FROM dbo.Models INNER JOIN dbo.Models_FormFactor ON 
        dbo.Models.FormFactor = dbo.Models_FormFactor.ID INNER JOIN
        dbo.Models_OEMs ON dbo.Models.Manufacturer = dbo.Models_OEMs.ID INNER JOIN
        dbo.Models_PriModels ON dbo.Models.PriModel = dbo.Models_PriModels.ID AND   
        dbo.Models_OEMs.ID = dbo.Models_PriModels.Manufacturer INNER JOIN
        dbo.Models_SecModels ON dbo.Models.SecModel = dbo.Models_SecModels.ID AND 
        dbo.Models_PriModels.ID = dbo.Models_SecModels.PriModel ORDER BY dbo.Models.ID 
        DESC"
        UpdateCommand="Procedure_UpdateModelsTables" UpdateCommandType="StoredProcedure"
        OnUpdated="OnDSUpdatedHandler"   
        InsertCommand="">
    </asp:SqlDataSource>
    
        CREATE PROCEDURE <Procedure_UpdateModelsTables> 
    
        <@Param int>,
        <@ManufacturerID int>,
        <@Manufacturer varchar(50)>,
        <@PriModelID int>,
        <@PriModel varchar(50)>,
        <@SecModelID int>,
        <@SecModel varchar(50)>,
        <@FormID int>,
        <@Form varchar(50)>,
        <@ModelsID int>,
        <@Active char(1)>,
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        -- Insert statements for procedure here
        --SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
        IF @param = 1     
            exec SP_OEMTable @ManufacturerID,@Manufacturer;
        IF @param = 2
           exec SP_PriModelsTable @PriModelID,@PriModel;
        IF @param = 3     
            exec SP_SecModelsTable @SecModelID,@SecModel;
        IF @param = 4
           exec SP_FormsTable @FormID,@Form;
        IF @param = 5
           exec SP_ModelsTable @ModelsID,@Active;
    
    END
    
    CREATE PROCEDURE SP_OEMTable
        @ManufacturerID int,
        @Manufacturer varchar(50)
    AS
    BEGIN  
        UPDATE Models_OEMs SET Manufacturer = @Manufacturer WHERE ID = @ManufacturerID;
    END
    
    CREATE PROCEDURE SP_PriModelsTable
        @PriModelID int,
        @ManufacturerID int,
        @PriModel varchar(50)
    AS
    BEGIN  
        UPDATE Models_PriModels SET PriModel = @PriModel WHERE ID = @PriModelID AND Manufacturer = @ManufacturerID;
    END
    CREATE PROCEDURE SP_SecModelsTable
        @SecModelID int,
        @PriModelID int,
        @SecModel varchar(50)
    AS
    BEGIN  
        UPDATE Models_SecModels SET SecModel = @SecModel WHERE ID = @SecModelID AND PriModel = @PriModelID;
    END
    CREATE PROCEDURE SP_FormsTable
        @FormID int,
        @Form varchar(50)
    AS
    BEGIN  
        UPDATE Models_FormFactor SET Form = @Form WHERE ID = @FormID;
    END
    CREATE PROCEDURE SP_ModelsTable
        @ModelsID int,
        @Active char(1)
    AS
    BEGIN  
        UPDATE Models SET Active = @Active WHERE ID = @ModelsID;
    END
    GO
    

1 个答案:

答案 0 :(得分:0)