-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsource.vb
More file actions
72 lines (64 loc) · 2.92 KB
/
Copy pathsource.vb
File metadata and controls
72 lines (64 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Option Explicit On
Option Strict On
' <Snippet1>
Imports System.Data.SqlClient
Module Module1
Sub Main()
' 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"
RunCommandAsynchronously(commandText, GetConnectionString())
Console.WriteLine("Press ENTER to continue.")
Console.ReadLine()
End Sub
Private Sub RunCommandAsynchronously(
ByVal commandText As String, ByVal connectionString 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.
Using connection As New SqlConnection(connectionString)
Try
Dim count As Integer = 0
Dim command As New SqlCommand(commandText, connection)
connection.Open()
Dim result As IAsyncResult = command.BeginExecuteNonQuery()
While Not result.IsCompleted
Console.WriteLine("Waiting ({0})", count)
' Wait for 1/10 second, so the counter
' does not consume all available resources
' on the main thread.
Threading.Thread.Sleep(100)
count += 1
End While
Console.WriteLine("Command complete. Affected {0} rows.",
command.EndExecuteNonQuery(result))
Catch ex As SqlException
Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message)
Catch ex As InvalidOperationException
Console.WriteLine("Error: {0}", ex.Message)
Catch ex As Exception
' You might want to pass these errors
' back out to the caller.
Console.WriteLine("Error: {0}", ex.Message)
End Try
End Using
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 Module
' </Snippet1>