[英]asynchronous function not executing sql CLR stored procedure
我有一個 CLR 存儲過程,我想從 C# 異步執行。
代碼如下:
private delegate void GeneratePayrollDelegate(string payProcessID);
public void GeneratePayroll(string payProcessID)
{
GeneratePayrollDelegate del = new GeneratePayrollDelegate(GeneratePayrollAsync);
del.BeginInvoke(payProcessID, null, null);
}
public void GeneratePayrollAsync(string payProcessID)
{
try
{
using (SqlConnection connection = new SqlConnection(DLConnectionStringHelper.GetConnectionString() + "; async=true;"))
{
using (SqlCommand cmd = new SqlCommand("proc_GeneratePayroll", connection))
{
cmd.CommandTimeout = 3600;
cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
}
}
catch (Exception ex) { _Exceptions.ManageExceptions(ex); }
}
如果從 sql 運行此存儲過程,則它會成功執行。
當它從上面的代碼執行時,當嘗試通過作為參數發送的 ID 檢索行時,它不會在 CLR 存儲過程中提供任何行。
需要幫忙!
您正在調用cmd.ExecuteNonQuery()
; 您應該調用BeginExecuteReader
來獲取結果,因為示例代碼可以是這樣的
private void Asynchronous(IAsyncResult asyncResult)
{
System.Data.SqlClient.SqlDataReader reader;
try
{
System.Data.SqlClient.SqlCommand command =
asyncResult.AsyncState as System.Data.SqlClient.SqlCommand;
reader = command.EndExecuteReader(asyncResult);
while (reader.Read())
{
}
reader.Close();
}
catch
{
}
}
public void GeneratePayrollAsync(string payProcessID)
{
try
{
using (SqlConnection connection = new SqlConnection("ConnectionString"))
{
using (SqlCommand command = new SqlCommand("proc_GeneratePayroll", connection))
{
command.CommandTimeout = 3600;
command.CommandType = CommandType.StoredProcedure;
//Set Your stored procedure parameter here
connection.Open();
command.BeginExecuteReader(Asynchronous, command, CommandBehavior.Default);
}
}
}
catch (Exception ex) { }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.