SqlConnectionStringBuilder.AsynchronousProcessing 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定布林值,指出使用這個連接字串建立的連接是否允許非同步處理 (Asynchronous Processing)。
public:
property bool AsynchronousProcessing { bool get(); void set(bool value); };
public bool AsynchronousProcessing { get; set; }
member this.AsynchronousProcessing : bool with get, set
Public Property AsynchronousProcessing As Boolean
屬性值
AsynchronousProcessing 屬性的值,如果未提供任何值,則為 false
。
範例
下列範例會擷取連接字串,並確認設定的連接字串允許非同步處理 (在此案例中,字串來自應用程式內的程式,但在生產應用程式中,連接字串 可能來自組態檔或其他來源。) 然後,此範例會在背景線程上執行異步操作、更新範例資料庫中的值。
using System.Data.SqlClient;
using System.Threading;
class Program
{
static void Main()
{
// Create a SqlConnectionStringBuilder instance,
// and ensure that it is set up for asynchronous processing.
SqlConnectionStringBuilder builder =
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;
string commandText =
"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();
}
private static string GetConnectionString()
{
// 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";
}
private static void RunCommandAsynchronously(string commandText,
string connectionString)
{
// 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 (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
int count = 0;
SqlCommand command = new SqlCommand(commandText, connection);
connection.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
while (!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.
Thread.Sleep(100);
count += 1;
}
Console.WriteLine("Command complete. Affected {0} rows.",
command.EndExecuteNonQuery(result));
}
catch (SqlException ex)
{
Console.WriteLine(
"Error {0}: System.Data.SqlClient.SqlConnectionStringBuilder",
ex.Number, ex.Message);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
catch (Exception ex)
{
// You might want to pass these errors
// back out to the caller.
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}
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
備註
此屬性會對應至連接字串內的 "Asynchronous Processing" 和 "async" 索引鍵。 為了要利用 SqlCommand 物件所提供的非同步處理,關聯之 SqlConnection 物件的連接字串內必須包含這個索引鍵/值組。