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)
有任何想法吗?
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |