3

この質問は前回の続きです。詳細は省きますが、データセットに 2 つの関連する 1 対多のテーブルを入力しています。だから、私の質問は、なぜこのコードがうまくいくのかということです

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";
}

2 番目の例の後、result.t0_DataAgency_R テーブルのみを入力しましたが、result.t01_ChoiceParam_R は入力しませんでした。なぜそうなるのですか?

前もって感謝します

4

2 に答える 2

13

DataTable.Loadリーダーを次の結果に自動的に進めます。したがって、への明示的な呼び出しを削除する必要がありますNextResult

意味:

using (DbDataReader reader = command.ExecuteReader())
{
    result.t0_DataAgency_R.Load(reader);
    result.t01_ChoiceParam_R.Load(reader);
}
于 2012-07-06T13:10:46.363 に答える