【问题标题】:asynchronous function not executing sql CLR stored procedure异步函数不执行 sql CLR 存储过程
【发布时间】:2011-06-06 09:21:44
【问题描述】:

我有一个 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 存储过程中没有给出任何行。

需要帮助!

【问题讨论】:

  • 在 CLR proc 中,函数之一是获取与此 payprocessID 相关的一些必需信息,它不返回任何行。当我尝试在位置 0 检索行时,它给出了范围异常
  • 所以,它仍然执行成功,但你得到了意想不到的结果?最好编辑问题。

标签: c# .net sql asynchronous sqlclr


【解决方案1】:

你打电话给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) {  }
}

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2018-04-29
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多