存储过程插入和更新语句工作但受影响的行是 -1?

jen*_*nas 0 sql vb.net sql-server ado.net stored-procedures

我正在开发一个可以通过 VB 程序添加赞助商的程序。当我在 SQL 中测试它们时,我用于添加和更新发起人的存储过程正在工作,但两者都抛出错误,因为受影响的行返回 -1。但是,当我查看我的数据库时,插入/更新确实有效。谁能帮我弄清楚如果插入有效,为什么受影响的行会返回-1?

它为两个存储过程带回 -1。这是添加赞助商的代码:

查询语句:

GO

CREATE PROCEDURE uspAddSponsor
    @intSponsorID AS INTEGER OUTPUT
   ,@strFirstName AS VARCHAR(50)
   ,@strLastName AS VARCHAR(50)
   ,@strStreetAddress AS VARCHAR(50)
   ,@strCity AS VARCHAR(50)
   ,@strState AS VARCHAR(50)
   ,@strZip AS VARCHAR(50)
   ,@strPhoneNumber AS VARCHAR(50)
   ,@strEmail AS VARCHAR(50)

AS 
SET NOCOUNT ON      --Report only errors
SET XACT_ABORT ON   --Terminate and rollback transaction on error

BEGIN TRANSACTION

    INSERT INTO TSponsors WITH (TABLOCKX) (strFirstName, strLastName, strStreetAddress, strCity, strState, strZip, strPhoneNumber, strEmail)
    VALUES               (@strFirstName, @strLastName, @strStreetAddress, @strCity, @strState, @strZip, @strPhoneNumber, @strEmail)

    SELECT @intSponsorID = MAX(intSponsorID) FROM TSponsors

COMMIT TRANSACTION

GO
Run Code Online (Sandbox Code Playgroud)

VB代码:

Private Sub AddSponsor(ByVal strFirstName As String, ByVal strLastName As String, ByVal strAddress As String, ByVal strCity As String, ByVal strState As String, ByVal strZip As String, ByVal strPhoneNumber As String, ByVal strEmail As String)
    Dim intRowsAffected As Integer
    Dim cmdAddSponsor As New OleDb.OleDbCommand()
    Dim intPKID As Integer

    Try
        'Open DB
        If OpenDatabaseConnectionSQLServer() = False Then

            ' No, warn the user ...
            MessageBox.Show(Me, "Database connection error." & vbNewLine &
                                "The application will now close.",
                                Me.Text + " Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error)

            ' and close the form/application
            Me.Close()
        End If

        'Text to call stored procedure
        cmdAddSponsor.CommandText = "EXECUTE uspAddSponsor '" & intPKID & "','" & strFirstName & "','" & strLastName & "','" & strAddress & "','" & strCity & "','" & strState & "','" & strZip & "','" & strPhoneNumber & "','" & strEmail & "'"
        cmdAddSponsor.CommandType = CommandType.StoredProcedure

        'Call stored procedure which will insert the record
        cmdAddSponsor = New OleDb.OleDbCommand(cmdAddSponsor.CommandText, m_conAdministrator)

        'This return is the # of rows affected
        intRowsAffected = cmdAddSponsor.ExecuteNonQuery()

        'Close DB
        CloseDatabaseConnection()

        'Let user know what happened
        If intRowsAffected > 0 Then
            MessageBox.Show("Sponsor successfully added")
        Else
            MessageBox.Show("Sponsor not added. Error")
        End If

        Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Close()
    End Try
End Sub

Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Dal*_*e K 5

根据官方文档

当在连接上设置 SET NOCOUNT ON 时(在执行命令之前或作为执行命令的一部分,或作为由执行命令启动的触发器的一部分),受单个语句影响的行停止对返回的受影响行的计数做出贡献通过这种方法。

我建议使用 SP 的返回值来指示 SP 是否工作,因为这是它的预期目的 - 因为您实际上并没有将受影响的行数用于其他任何事情。