获取VB中存储过程生成的主键值

时间:2015-03-10 14:17:21

标签: sql vb.net

注意:该存储过程由公司使用,我无法更改。

以下是存储过程的一部分:

    DECLARE @tmp_PERSON TABLE(PERSON_PK INT);
        INSERT INTO [Ks4_ENTITY_PERSON]
                   ([TITLE_FK]
                   ,[INITIALS]
                   ,[FIRSTNAME]
                   ,[MIDDLENAME]
                   ,[KNOWNAS]
                   ,[SURNAME]
                   ,[GENDER_FK]
                   ,[DATEOFBIRTH]
                   ,[HOME_LANGUAGE_FK]
                   ,[IDNUMBER_TYPE_FK]
                   ,[IDNUMBER]
                   ,[MATURITY_FK]
                   ,[MARITAL_STATUS_FK]
                   ,[MARITAL_DATE]
                   ,[MARITAL_REGIME_FK]
                   ,[CITIZENSHIP1_COUNTRY_FK]
                   ,[CITIZENSHIP2_COUNTRY_FK]
                   ,[TAX_NUMBER]
                   ,[SUFFIX_FK]
                   ,[SALUTATION_FK]
                   ,[RESIDENT_COUNTRY_FK]
                   ,[IS_PENSIONER]
                   ,[OCCUPATION_FK])
             OUTPUT inserted.PERSON_PK INTO @tmp_PERSON <---- THIS LINE??!?
             VALUES
                   (@TITLE_FK
                   ,@INITIALS
                   ,@FIRSTNAME
                   ,@MIDDLENAME
                   ,@KNOWNAS
                   ,@SURNAME
                   ,@GENDER_FK
                   ,@DATEOFBIRTH
                   ,@HOME_LANGUAGE_FK
                   ,@IDNUMBER_TYPE_FK
                   ,@IDNUMBER
                   ,@MATURITY_FK
                   ,@MARITAL_STATUS_FK
                   ,@MARITAL_DATE
                   ,@MARITAL_REGIME_FK
                   ,@CITIZENSHIP1_COUNTRY_FK
                   ,@CITIZENSHIP2_COUNTRY_FK
                   ,@TAX_NUMBER
                   ,@SUFFIX_FK
                   ,@SALUTATION_FK
                   ,@RESIDENT_COUNTRY_FK
                   ,@IS_PENSIONER
                   ,@OCCUPATION_FK)  

如何获取插入PERSON_PK @tmp_PERSON的值@tmp_Person 从我的VB应用程序?

注意:@VAR OUTPUT是一个动态声明的表,而不是预定义的{{1}}类型

2 个答案:

答案 0 :(得分:0)

以下是在VB.NET中从输出参数创建和检索数据的代码:

Private Function test()
        Dim cmd As New SqlCommand("Your_SP_Name_Here", _
                                  New SqlConnection(ConfigurationManager.ConnectionStrings("your_conn_settings_id").ConnectionString)) 'This SQL Connection should be replaced with a connection specific to your SQL Server
        cmd.CommandType = CommandType.StoredProcedure

        With cmd.Parameters
            .Add("@PERSON_PK", SqlDbType.SmallInt, Nothing).Direction = ParameterDirection.Output
        End With

        Dim newid As Integer = 0

        Try
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            newid = cmd.Parameters("@PERSON_PK").Value

        Catch sql_ex As SqlException
            cmd.Connection.Close()
            cmd = Nothing
            Throw sql_ex
        Catch ex As Exception
            cmd.Connection.Close()
            cmd = Nothing
            Throw ex
        End Try

        cmd.Connection.Close()
        cmd = Nothing

        Return newid ' <-- YOUR NEW ID IS HERE
End Function

答案 1 :(得分:0)

您可以更改存储过程以返回@@ identity(https://msdn.microsoft.com/en-us/library/ms187342.aspx)然后您可以使用本文更新VB代码以获取值How to get a return value from a stored procedure in VB.NET