my output parameters are always null when i use BeginExecuteNonQuery

﹥>﹥吖頭↗ 提交于 2019-12-11 00:27:13

问题


I have a stored procedure that returns a varchar(160) as an output parameter of a stored procedure.

Everything works fine when i use ExecuteNonQuery, i always get back the expected value.

However, once i switch to use BeginExecuteNonQuery, i get a null value for the output.

I am using connString + "Asynchronous Processing=true;" in both cases.

Sadly the BeginExecuteNonQuery is about 1.5 times faster in my case...but i really need the output parameter.

Thanks!

EDIT:This is how i am processing the BeginExecuteNonQuery callback (i am using .net 4.0...)

    Dim resp as String=""
    cmd.BeginExecuteNonQuery(Sub(result As IAsyncResult)
                                 Dim c As SqlCommand = Nothing
                                 Try
                                     c = CType(result.AsyncState, SqlCommand)
                                     c.EndExecuteNonQuery(result)
                                     **resp = CStr(c.Parameters("@response").Value)**
                                 Catch ex As Exception
                                     WriteLog("ERR - LogRequest - " & ex.Message)
                                 Finally
                                     c.Connection.Close()
                                     c.Dispose()
                                 End Try
                             End Sub, cmd)

回答1:


If you use BeginExecuteNonQuery your code does not wait for the query to execute before continuing, that is why you don't have the output parameter. In order to retrieve the out parameter you need to specify an AsyncCallback delegate that runs when the query has finished executing. Also are you sure that BeginExecuteNonQuery is really faster and that the perceived performance increase is not just because the process is just not waiting for the query to execute. The point of Async queries is where you want to fire off a long bit of processing such as to produce a complex report, then do something later once it is complete, e.g. email the user to tell them their report has been processed.




回答2:


When you use BeginExecuteNonQuery the query runs in the background, and your code continues to run. What is probably happening is that you are looking at the result before the query has finished executing. This is from the msdn help for BeginExecuteNonQuery:

IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
    Console.WriteLine("Waiting ({0})", count++);
    // Wait for 1/10 second, so the counter
    // does not consume all available resources 
    // on the main thread.
    System.Threading.Thread.Sleep(100);
}
Console.WriteLine("Command complete. Affected {0} rows.", 
     command.EndExecuteNonQuery(result));

So the result is only properly available once IsCompleted is true.

Note that this is just an example from the docs, the real use of the async functions is to allow the rest of your app (eg. your UI) to continue to function whilst a long query is being run.



来源:https://stackoverflow.com/questions/2887570/my-output-parameters-are-always-null-when-i-use-beginexecutenonquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!