Use this code and when the command is finished it will return the error:
SqlCommand.EndExecuteNonQuery(result)
and this is my full class code:
Imports System.Data.SqlClient
Imports System.DirectoryServices.ActiveDirectory
Class clsExecuteAsync
Public Event EnProceso(Identificador As Integer, Mensaje As String)
Public Event Finalizado(IDentificador As Integer, Mensaje As String)
Public Event CancelarProcesoEnEjecucion(Identificador As Integer, ByRef Cancel As Boolean)
Dim Cancelar As Boolean
Sub CancelarProceso()
Cancelar = True
End Sub
Function test() As Boolean
' This is a simple example that demonstrates the usage of the
' BeginExecuteNonQuery functionality.
' The WAITFOR statement simply adds enough time to prove the
' asynchronous nature of the command.
Dim commandText As String = "UPDATE Production.Product SET ReorderPoint = ReorderPoint + 1 " & "WHERE ReorderPoint Is Not Null;" & "WAITFOR DELAY '0:0:3';" & "UPDATE Production.Product SET ReorderPoint = ReorderPoint - 1 " & "WHERE ReorderPoint Is Not Null"
Return (RunCommandAsynchronously(0, commandText, GetConnectionString()))
Console.WriteLine("Press ENTER to continue.")
Console.ReadLine()
End Function
Function ExecuteAsync(Identificador As Integer, Sql As String, Optional CadenaConexion As String = "") As String
If CadenaConexion = "" Then
CadenaConexion = clsIni.LeeIni("Provider")
End If
Return RunCommandAsynchronously(Identificador, Sql, CadenaConexion)
End Function
Function RunCommandAsynchronously(Identificador As Integer, commandText As String, connectionString As String) As String
' Given command text and connection string, asynchronously execute
' the specified command against the connection. For this example,
' the code displays an indicator as it is working, verifying the
' asynchronous behavior.
Dim Resultado As String = ""
Try
Dim connection As SqlConnection
Dim SqlCommand As SqlCommand
connection = New SqlConnection(connectionString)
Dim count As Integer = 0
'testint to catch the error, but not run for me
AddHandler connection.InfoMessage, AddressOf ErrorEnConexion
SqlCommand = New SqlCommand(commandText, connection)
connection.Open()
Dim result As IAsyncResult = SqlCommand.BeginExecuteNonQuery()
While Not result.IsCompleted
Console.WriteLine("Waiting ({0})", count = count + 1)
' Wait for 1/10 second, so the counter
' does not consume all available resources
' on the main thread.
System.Threading.Thread.Sleep(100)
RaiseEvent EnProceso(Identificador, commandText)
Application.DoEvents()
If Cancelar Then
Cancelar = False
'cancelar
Dim Cancel As Boolean = False
RaiseEvent CancelarProcesoEnEjecucion(Identificador, Cancel)
If Cancel = False Then
Resultado = "Cancelado"
GoTo SALIR
End If
End If
End While
'Console.WriteLine("Command complete. Affected {0} rows.", Command.EndExecuteNonQuery(Result))
' MsgBox("El comando se ejecutó. " & SqlCommand.EndExecuteNonQuery(result), MsgBoxStyle.Information)
'detect error: this code lunch and error: Cath with try cacth code
SqlCommand.EndExecuteNonQuery(result)
RaiseEvent Finalizado(Identificador, SqlCommand.EndExecuteNonQuery(result))
Resultado = "OK"
Catch ex As SqlException
Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message)
Resultado = ex.Message
Catch ex As InvalidOperationException
Console.WriteLine("Error: {0}", ex.Message)
Resultado = ex.Message
Catch ex As Exception
' You might want to pass these errors
' back out to the caller.
Console.WriteLine("Error: {0}", ex.Message)
Resultado = ex.Message
End Try
SALIR:
Return Resultado
End Function
Private Sub ErrorEnConexion(sender As Object, e As SqlInfoMessageEventArgs)
MsgBox(e.Message)
End Sub
Private Function GetConnectionString() As String
' To avoid storing the connection string in your code,
' you can retrieve it from a configuration file.
' If you have not included "Asynchronous Processing=true" in the
' connection string, the command is not able
' to execute asynchronously.
Return "Data Source=(local);Integrated Security=SSPI;" & "Initial Catalog=AdventureWorks; Asynchronous Processing=true"
End Function
End Class
NonQuery
method - it really would be clearer if you used the actual names... editing... – Event