【问题标题】:How to get last inserted identity in Formview Inserted event using VB.net如何使用 VB.net 在 Formview Inserted 事件中获取最后插入的标识
【发布时间】:2009-09-21 08:06:36
【问题描述】:

我正在使用 VB.net (FormView 和 ObjectDataSource) 和 Sql Server 2005。

我想在 FormView1_ItemInserted

上的表格中最后插入 @@identity
Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted

End Sub

我的问题是我想在 FormView1_ItemInserted 之后将我的 FormView 重定向到只读模式,但为此我需要以只读模式显示插入的记录,并且只有在我最后插入时才有可能 @@身份。你能告诉我我需要在我的应用程序、过程和代码中做哪些更改以实现这一目标。

请提供示例代码!使用VB.net

谢谢。

最好的问候, 女士

【问题讨论】:

    标签: sql-server vb.net


    【解决方案1】:

    我用下面的逻辑解决了我上面的问题

    我更改了我的 SQL 插入过程并添加了新参数

    @OrgID int OUTPUT 
    

    插入命令之后

    我用过

    SET @OrgID = SCOPE_IDENTITY()
    
    RETURN
    

    在重新配置我的 ObjectDataSource 后,在我的应用程序中,我得到了以下参数

        <asp:Parameter ConvertEmptyStringToNull="true" Name="OrgID" Type="Int32" 
            Direction="Output" />
    

    在我的objectdatasource中插入参数

    我在我的 ObjectDataSource 插入事件中编写以下代码。

    Protected Sub RAOOrganisationDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles RAOOrganisationDataSource.Inserted
        Dim OrgID As Integer = e.OutputParameters("OrgID")
        Session("OrgID") = OrgID
    End Sub
    

    因此我在 Session 中获得了最新插入的 OrgID。

    干杯!

    如果在使用上述概念时有任何错误,请告诉我

    【讨论】:

      【解决方案2】:

      实际上,最简单的方法是将 onInserted 事件添加到您的数据源,在我的例子中是 LinqDatasource。

      protected void lnqInsert_Inserted(object sender, LinqDataSourceStatusEventArgs e)
      {
                  Session["RecentInsertedClass"] = ((Class)e.Result).ID;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        相关资源
        最近更新 更多