"There is already an open DataReader associated with this Command which must be closed first"



我的代码在这里返回错误"已经有一个开放的数据阅读器与此命令相关联,必须先关闭",我已经在我的web上添加了代码"MultipleActiveResultSets=true"。配置

        ReportViewer1.Visible = true;
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        var sqlConnection = new SQLConnectionStrings();
        var conReport = new SqlConnection(sqlConnection.strConn);
        var cmdReport = new SqlCommand();
        var myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = cmdReport;
        SqlDataReader drReport;
        DataSet dsReport = new DataSet();
        //PerfLogDataSet();

        cmdReport.CommandType = CommandType.Text;
        cmdReport.Connection = conReport;
        conReport.Open();
        cmdReport.CommandText = baseQuery;
        drReport = cmdReport.ExecuteReader();
        myDataAdapter.Fill(dsReport); //where the error occurs
        dsReport.Tables[0].Load(drReport);
        drReport.Close();
        conReport.Close();

您试图同时使用SqlDataAdapterSqlDataReader与相同的SqlCommand

SqlDataAdapter将结果集加载到DataSetDataTable中。SqlDataReader用于在结果集上每次循环一条记录,recordset样式。它们是访问数据的两种不同的方法,具有两种不同的目的,两者都使用是没有意义的。

如果你想加载dsReport,请删除

SqlDataReader drReport;
drReport = cmdReport.ExecuteReader();
dsReport.Tables[0].Load(drReport);
drReport.Close();

当你已经使用

加载适配器时
myDataAdapter.Fill(dsReport);

如果您想填充数据集,请尝试此代码…

    ReportViewer1.Visible = true;
    ReportViewer1.ProcessingMode = ProcessingMode.Local;
    var sqlConnection = new SQLConnectionStrings();
    var conReport = new SqlConnection(sqlConnection.strConn);
    var cmdReport = new SqlCommand(baseQuery);
    var myDataAdapter = new SqlDataAdapter(cmdReport,sqlConnection );
    DataSet dsReport = new DataSet();
    conReport.Open();
    myDataAdapter.Fill(dsReport);
    conReport.Close();

但如果你想要其他的,描述它,更多;)

相关内容

  • 没有找到相关文章

最新更新