一尘不染

DbDataReader,NextResult()并填充多个表

sql

这个问题是我上一个问题的延续。无需赘述,我将用2个相关的一对多表填充数据集。所以,我现在的问题是-为什么这段代码能正常工作

public DataAgencyR_DataSet SelectOne(int id)
{
    DataAgencyR_DataSet result = new DataAgencyR_DataSet();
    using (DbCommand command = Connection.CreateCommand())
    {
        try
        {               
            command.CommandText = SqlStrings.SelectDataAgencyR_SelectOne();

            var param = ParametersBuilder.CreateByKey(command, "ID_DeclAgenc", id, "ID_DeclAgenc");
            command.Parameters.Add(param);
            Connection.Open();
            using (DbDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    System.Diagnostics.Trace.WriteLine(String.Format("{0}-{1}", reader[0], reader[1]));
                }
                System.Diagnostics.Trace.WriteLine("-------------");
                reader.NextResult();
                while (reader.Read())
                {
                    System.Diagnostics.Trace.WriteLine(String.Format("{0}-{1}", reader[0], reader[1]));
                }
            }

        }
        catch (DbException e)
        {
            Logger.Error(e.Message, e);
            throw new DataAccessException("Error occurs while SelectOne method porcessed", e);
        }
        finally
        {
            if (Connection.State != ConnectionState.Closed)                     Connection.Close();
        }
    }
    return result;
}

public static string SelectDataAgencyR_SelectOne()
{
    return "SELECT a.* FROM t0_DataAgency_R a WHERE a.SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc;" +
                   "SELECT c.* FROM t01_ChoiceParam_R c JOIN t0_DataAgency_R a on a.ID_DeclAgenc = c.ID_DeclAgenc WHERE SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc";
}

这不是

public DataAgencyR_DataSet SelectOne(int id)
{
    DataAgencyR_DataSet result = new DataAgencyR_DataSet();

    using (DbCommand command = Connection.CreateCommand())
    {
        try
        {               
            command.CommandText = SqlStrings.SelectDataAgencyR_SelectOne();             
            var param = ParametersBuilder.CreateByKey(command, "ID_DeclAgenc", id, "ID_DeclAgenc");
            command.Parameters.Add(param);
            Connection.Open();
            using (DbDataReader reader = command.ExecuteReader())
            {
                result.t0_DataAgency_R.Load(reader);
                reader.NextResult();
                result.t01_ChoiceParam_R.Load(reader);
            }
        }
        catch (DbException e)
        {
            Logger.Error(e.Message, e);
            throw new DataAccessException("Error occurs while SelectOne method porcessed", e);
        }
        finally
        {
            if (Connection.State != ConnectionState.Closed) Connection.Close();
        }
    }
    return result;
}


public static string SelectDataAgencyR_SelectOne()
{
    return "SELECT a.* FROM t0_DataAgency_R a WHERE a.SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc;" +
                   "SELECT c.* FROM t01_ChoiceParam_R c JOIN t0_DataAgency_R a on a.ID_DeclAgenc = c.ID_DeclAgenc WHERE SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc";
}

在第二个示例之后,我仅填写了result.t0_DataAgency_R表,但没有填写result.t01_ChoiceParam_R。为什么会这样呢?

提前致谢


阅读 235

收藏
2021-03-08

共1个答案

一尘不染

DataTable.Load自动将阅读器移至下一个结果。因此,您应该删除对的显式调用NextResult

意义:

using (DbDataReader reader = command.ExecuteReader())
{
    result.t0_DataAgency_R.Load(reader);
    result.t01_ChoiceParam_R.Load(reader);
}
2021-03-08