SqlDataSource运行存储过程两次

时间:2016-04-12 10:50:16

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

我有下表

[Department]  [Location]    [Project]      [Job Title]   [Qty]
-------------------------------------------------------------  
HR            Jeddah        Andalusia       JT2            5  
IT            Jeddah        Head Office     JT1            3

我尝试使用SqlDataSource运行已排序的过程,如下所示

CREATE PROCEDURE [dbo].[MManPowerUpdate]
    @Dept     nvarchar(350),
    @Loca     nvarchar(350),
    @Proj     nvarchar(350),
    @JobT     nvarchar(350)
AS

   IF (SELECT QTY FROM [MANPOWER] WHERE ([Department] = @Dept) AND ([Location] = @Loca) AND ([Project] = @Proj) AND ([JobTitle] = @JobT)) > 0
       Begin
         SELECT  'Operation Completed Successfully.' AS [Result]
         UPDATE [MANPOWER] SET QTY = QTY - 1 WHERE ([Department] = @Dept) AND ([Location] = @Loca) AND ([Project] = @Proj) AND ([JobTitle] = @JobT)
       End
   Else
       Begin
         SELECT 'Can''t Execute Operation.' AS [Result]
       End
Return 0  

ASP.net代码:

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:_JOOBERConnectionString %>" SelectCommandType="StoredProcedure" SelectCommand="MManPowerUpdate">
        <SelectParameters>
            <asp:ControlParameter ControlID="txtDept" Name="Dept" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="txtLoca" Name="Loca" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="txtProj" Name="Proj" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="txtJobT" Name="JobT" PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="Result" HeaderText="Result" ReadOnly="True" SortExpression="Result" />
        </Columns>
    </asp:GridView>

vb.net代码背后:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SqlDataSource1.Select(New DataSourceSelectArguments)
End Sub

当我使用此值执行代码时。

@Dept  = HR
@Loca  = Jeddah
@proj  = Andalus
@JobT  = JT2 

我希望更新后数量为4,并检索“操作成功完成”。&#39;但我以后在更新后得到3,这意味着当我调用该程序时,更新语句运行了两次。 所以我希望能找到解决方案。

1 个答案:

答案 0 :(得分:1)

问题是您的Select查询还会更新您的数据。由于在生命周期中,PreRender上的Select查询为automatically called,因此单击按钮时会执行两次递减操作:一次在按钮单击事件处理程序中,一次在PreRender中(并且每次打开表单时可能会减少QTY,即使您没有按Button1)。

一种解决方案是将MManPowerUpdate定义为更新查询:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:_JOOBERConnectionString %>" SelectCommandType="StoredProcedure" UpdateCommand="MManPowerUpdate">
    <UpdateParameters>
        <asp:ControlParameter ControlID="txtDept" Name="Dept" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="txtLoca" Name="Loca" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="txtProj" Name="Proj" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="txtJobT" Name="JobT" PropertyName="Text" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

单击按钮时调用它:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SqlDataSource1.Update()
    GridView1.DataBind()
End Sub

您可以定义另一个Select查询,该查询将获取数据并将其显示为GridView,而不进行修改。