当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET SqlConnectionStringBuilder.AsynchronousProcessing属性代码示例

本文整理汇总了VB.NET中System.Data.SqlClient.SqlConnectionStringBuilder.AsynchronousProcessing属性的典型用法代码示例。如果您正苦于以下问题:VB.NET SqlConnectionStringBuilder.AsynchronousProcessing属性的具体用法?VB.NET SqlConnectionStringBuilder.AsynchronousProcessing怎么用?VB.NET SqlConnectionStringBuilder.AsynchronousProcessing使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Data.SqlClient.SqlConnectionStringBuilder的用法示例。


在下文中一共展示了SqlConnectionStringBuilder.AsynchronousProcessing属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Module1

' 导入命名空间
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
开发者ID:VB.NET开发者,项目名称:System.Data.SqlClient,代码行数:73,代码来源:SqlConnectionStringBuilder.AsynchronousProcessing


注:本文中的System.Data.SqlClient.SqlConnectionStringBuilder.AsynchronousProcessing属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。