jueves, 3 de julio de 2008
Operaciones de Acceso a datos Asincronico 2
Las operaciones asincrónicas no solo nos permiten realizar más un una consulta a la vez, también tenemos la posibilidad de hacer otros comandos y sacarle más provecho a nuestro servidor haciendo varias operaciones al mismo tiempo, en el siguiente ejemplo se puede ver que tiene 3 updates al mismo tiempo.Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
' Add the remaining code to the form's class:
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim connection1 As New SqlConnection(GetConnectionString())
Dim connection2 As New SqlConnection(GetConnectionString())
Dim connection3 As New SqlConnection(GetConnectionString())
Dim commandText1 As String = _
"UPDATE Production.Product " & _
"SET ReorderPoint = ReorderPoint + 1 " &_
"WHERE ReorderPoint Is Not Null;" & _
"WAITFOR DELAY '0:0:01';" & _
"UPDATE Production.Product " & _
"SET ReorderPoint = ReorderPoint - 1 " _
"WHERE ReorderPoint Is Not Null"
Dim commandText2 As String = _
"UPDATE Production.Product " & _
"SET ReorderPoint = ReorderPoint + 1 " & _
"WHERE ReorderPoint Is Not Null;" & _
"WAITFOR DELAY '0:0:05';" & _
"UPDATE Production.Product " & _
"SET ReorderPoint = ReorderPoint - 1 " &_
"WHERE ReorderPoint Is Not Null"
Dim commandText3 As String = _
"UPDATE Production.Product " & _
"SET ReorderPoint = ReorderPoint + 1 " & _
"WHERE ReorderPoint Is Not Null;" & _
"WAITFOR DELAY '0:0:10';" & _
"UPDATE Production.Product " & _
"SET ReorderPoint = ReorderPoint - 1 " & _
"WHERE ReorderPoint Is Not Null"
Dim waitHandles(2) As WaitHandle
Try
connection1.Open()
Dim command1 As New SqlCommand(commandText1, connection1)
Dim result1 As IAsyncResult = command1.BeginExecuteNonQuery()
waitHandles(0) = result1.AsyncWaitHandle
connection2.Open()
Dim command2 As New SqlCommand(commandText2, connection2)
Dim result2 As IAsyncResult = command2.BeginExecuteNonQuery()
waitHandles(1) = result2.AsyncWaitHandle
connection3.Open()
Dim command3 As New SqlCommand(commandText3, connection3)
Dim result3 As IAsyncResult = command3.BeginExecuteNonQuery()
waitHandles(2) = result3.AsyncWaitHandle
Dim result As Boolean = WaitHandle.WaitAll(waitHandles, 60000, False)
If result Then
Dim rowCount1 As Long = command1.EndExecuteNonQuery(result1)
TextBox1.Text = "Completed " & _
System.DateTime.Now.ToLongTimeString()
Dim rowCount2 As Long = command2.EndExecuteNonQuery(result2)
TextBox2.Text = "Completed " & _
System.DateTime.Now.ToLongTimeString()
Dim rowCount3 As Long = command3.EndExecuteNonQuery(result3)
TextBox3.Text = "Completed " & _
System.DateTime.Now.ToLongTimeString()
Else
Throw New Exception("Timeout")
End If
Catch ex As Exception
TextBox4.Text = ex.ToString
End Try
connection1.Close()
connection2.Close()
connection3.Close()
End Sub
Suscribirse a:
Comentarios de la entrada (Atom)
No hay comentarios.:
Publicar un comentario