我在嘗試使用VB.NET,SQL命令和參數再次執行DELETE查詢SQL Server表時遇到一個奇怪的問題。Sqlcommand參數未執行
我有以下代碼:
Try
sqlCommand.Transaction = transaction1
sqlCommand.Connection = conn
sqlCommand.CommandText = sqlQuery
sqlCommand.Parameters.Add("@userID", SqlDbType.Int).Value = Convert.ToInt32(userID)
sqlCommand.Parameters.Add("@groupID", SqlDbType.Int).Value = Convert.ToInt32(groupID)
''#Delete the user from the group.
MessageBox.Show("User: " + Convert.ToString(userID) + " Group: " + Convert.ToString(groupID))
MessageBox.Show("Param, UserID: " + sqlCommand.Parameters.Item(0).Value.ToString)
MessageBox.Show("Param, GroupID: " + sqlCommand.Parameters.Item(1).Value.ToString)
return_deleteUser = sqlCommand.ExecuteNonQuery()
Catch ex As Exception
transaction1.Rollback()
Dim hr As Integer = Marshal.GetHRForException(ex)
MsgBox("Removal of user from group has failed: " + ex.Message() & hr)
End Try
它執行下面的SQL查詢:
Dim sqlQuery As String = "DELETE FROM MHGROUP.GROUPMEMS WHERE USERNUM [email protected] AND GROUPNUM [email protected]"
我的問題是,代碼執行時,沒有在所有報告的錯誤。我運行了SQL事件探查器,查詢沒有出現在跟蹤列表中。我添加的三個消息框全部返回正確的值,並且如果我要使用查詢成功的值對錶執行SQL查詢。用戶ID和組ID都是3位整數。
任何人都可以提出爲什麼代碼不按預期工作,或任何進一步的調試,我可以用來逐步通過代碼?理想情況下,我很想看到已完成的參數的完成SQL查詢,但我還沒有發現如何做到這一點。
編輯: 我在後面的代碼下面來檢查執行的所有成功處理:
If return_insertEvent > 0 And return_updateUser > 0 And return_nextSID > 0 And return_deleteUser > 0 Then
MessageBox.Show("Success")
return_removeADGroup = RemoveUserFromGroup(userID, groupName)
MessageBox.Show("Remove FS User from AD Group: " + return_removeADGroup)
transaction1.Commit()
transaction2.Commit()
transaction3.Commit()
transaction4.Commit()
returnResult = 1
Else
transaction1.Rollback()
transaction2.Rollback()
transaction3.Rollback()
transaction4.Rollback()
returnResult = 0
End If
如果您需要任何進一步的信息,請不要在我接觸猶豫。
你有驗證器transaction.commit叫? – 2010-08-14 03:17:20