开发者

Will Sqldatareader with Using Statement ever return an Output parameter?

开发者 https://www.devze.com 2023-03-29 10:47 出处:网络
Here\'s a simple code snippet of a larger function as an example. Usi开发者_如何学Gong conn as New SqlConnection(\"conn string\")

Here's a simple code snippet of a larger function as an example.

Usi开发者_如何学Gong conn as New SqlConnection("conn string")
    Using find as new SqlCommand("ExampleProc",conn)
        Dim rParam as new SqlParameter("@RESULT",SqlDbType.Int)
        rParam.Direction = ParameterDirection.Output
        find.Pareameters.Add(rParam)

        Using f as SqlDataReader = find.ExecuteReader
              'Do stuff with datareader
        End Using

        updateResult.Success = Convert.ToBoolean(find.Parameters("@RESULT").Value)
    End Using
  End Using

I know the Output parameter is returned after the SqlDataReader is closed. From what I think I know, the Using statement will call .Dispose on the SqlDataReader, so will that basically wipe out the Output parameter? If so, what is the best method to call a SqlDataReader that also contains Output parameters that closes and disposes everything correctly? I couldn't find any specific info or examples online in my searching. Thanks!


Let me add that based on what I've read you only have access to the Output parameter using a SqlDataReader after you call .Close on the SqlDataReader.


You really only need the value returned in the output parameter.

You can copy the value to a variable declared outside the Using block and return that, or return the value directly, as soon as you have access to it.


not sure that I understand what you want to achieve but this seems a better approach

    updateResult.Success = False

    Using f as SqlDataReader = find.ExecuteReader
          'Do stuff with datareader
          updateResult.Success = Convert.ToBoolean(find.Parameters("@RESULT").Value)
    End Using

You read the result within the Using statement and if anything fails you preset it to false...

0

精彩评论

暂无评论...
验证码 换一张
取 消