使用 using 语句的 Sqldatareader 会返回输出参数吗?
以下是一个较大函数的简单代码片段作为示例。
Using 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
我知道 SqlDataReader 关闭后会返回 Output 参数。据我所知,Using 语句将调用 SqlDataReader 上的 .Dispose
,那么这会基本上消除 Output 参数吗?如果是这样,调用还包含正确关闭和处理所有内容的输出参数的 SqlDataReader 的最佳方法是什么?我在网上搜索时找不到任何具体信息或示例。谢谢!
让我补充一点,根据我所读到的内容,在 SqlDataReader 上调用 .Close 后,您只能使用 SqlDataReader 访问输出参数。
Here's a simple code snippet of a larger function as an example.
Using 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上只需要输出参数中返回的值。
您可以将该值复制到在
Using
块外部声明的变量并返回该变量,或者在您有权访问该值后立即返回该值。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.不确定我明白你想要实现什么,但这似乎是一个更好的方法
你在
Using
语句中读取结果,如果有任何失败,你将其预设为 false...not sure that I understand what you want to achieve but this seems a better approach
You read the result within the
Using
statement and if anything fails you preset it to false...