Export (0) Print
Expand All

SqlConnectionStringBuilder.AsynchronousProcessing Property

Gets or sets a Boolean value that indicates whether asynchronous processing is allowed by the connection created by using this connection string.

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Property AsynchronousProcessing As Boolean

Property Value

Type: System.Boolean
Caution noteCaution

This property is ignored beginning in .NET Framework 4.5. For more information about SqlClient support for asynchronous programming, see Asynchronous Programming.

The value of the AsynchronousProcessing property, or false if no value has been supplied.

This property corresponds to the "Asynchronous Processing" and "async" keys within the connection string. In order to take advantage of the asynchronous processing provided by the SqlCommand object, this key/value pair must be included within the connection string of the associated SqlConnection object.

The following example retrieves a connection string and verifies that the connection string is configured to allow for asynchronous processing. (In this case, the string comes from a procedure within the application, but in a production application, the connection string might come from a configuration file, or some other source.) Then, the example performs an asynchronous operation, updating values within a sample database on a background thread.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft