-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsource.vb
More file actions
79 lines (70 loc) · 3.08 KB
/
Copy pathsource.vb
File metadata and controls
79 lines (70 loc) · 3.08 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
73
74
75
76
77
78
Option Explicit On
Option Strict On
Imports System.Data
' <Snippet1>
Imports System.Data.SqlClient
Imports System.Threading
Module Module1
Sub Main()
' Create a SqlConnectionStringBuilder instance,
' and ensure that it is set up for asynchronous processing.
Dim builder As _
New SqlConnectionStringBuilder(GetConnectionString())
' Asynchronous method calls won't work unless you
' have added this option, or have added
' the clause "Asynchronous Processing=True"
' to the connection string.
builder.AsynchronousProcessing = True
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, builder.ConnectionString)
Console.WriteLine("Press any key to finish.")
Console.ReadLine()
End Sub
Private Function GetConnectionString() As String
' To avoid storing the connection string in your code,
' you can retrieve it from a configuration file.
Return "Data Source=(local);Integrated Security=SSPI;" & _
"Initial Catalog=AdventureWorks"
End Function
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's 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
' doesn't 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}: System.Data.SqlClient.SqlConnectionStringBuilder", _
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
End Module
' </Snippet1>