.net - 如果在 SqlDataReader 关闭之前我们无法检索输出参数,那么为什么......?

标签 .net asp.net ado.net sqldatareader sqlconnection

1)

While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

如果上述说法正确,那么为什么以下方法能够在阅读器关闭之前从输出参数中检索值:

    public int Something()
    {
        using (SqlConnection con = new SqlConnection(this.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("some_procedure", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteReader();
            return (int)cmd.Parameters["@ID"].Value;
        }
    }

2)

You can reset the CommandText property and reuse the SqlCommand object. However, you must close the SqlDataReader before you can execute a new or previous command.

为什么在执行新命令之前必须关闭sqldatareader?

谢谢

最佳答案

在第一个问题中,您正在执行 NonQuery - 因此在获取输出参数之前没有需要关闭的阅读器。

对于第二个问题,你就做吧。当一个阅读器打开时,该命令不会让您调用另一阅读器。

关于.net - 如果在 SqlDataReader 关闭之前我们无法检索输出参数,那么为什么......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2632619/

相关文章:

c# - 考虑到必须从数据库中获取答案,如何从多个单选按钮检查正确答案

.net - 我可以在桌面窗口上使用 DPWS 吗?

.net - System.Net.WebClient 与代理身份验证 407 错误

c# - 文本框只允许字母

c# - 具有用户分配的托管标识的 Azure 应用服务使应用程序崩溃

.net - ConvertEmptyStringToNull 属性

c# - 将 .net decimal 类型转换为 tsql decimal(3,3)

c# - LinQ 查询 where 条件

c# - 带有 SQL Server 数据库和 Entity Framework 错误的 Windows 应用程序部署

c# - ASP.NET 事件未在 <div> 标记中触发