获取SQL数据库中新插入记录的ID

时间:2014-09-10 17:45:03

标签: sql sql-server vb.net

我正在使用detailsview将记录插入数据库,我想在插入后显示一条确认信息,显示新记录的ID。我使用以下代码:

Protected Sub DetailsView1_ItemInserted(sender As Object, e As DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted


    Dim strConnString As String = ConfigurationManager.ConnectionStrings("houses").ConnectionString
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()

    cmd.CommandText = "SELECT @@IDENTITY"

    cmd.Connection = con
    Try
        con.Open()
        Dim obj As Object = cmd.ExecuteScalar()
        Response.Write("<div class=newlisting> The Record ")
        Response.Write("<span class=deletedlistinghighlight>")
        Response.Write(obj.ToString)
        Response.Write("</span> Was Added.</div>")


    Catch ex As Exception
        Throw ex
    Finally
        con.Close()
        con.Dispose()
    End Try

End Sub

确认显示在页面顶部,但不会显示ID,我不确定我缺少的是什么。

0 个答案:

没有答案