コード例 #1
0
    public void CanExecuteCommandWithMultipleResultSets()
    {
        // Arrange
        // Important to initialize datareader BEFORE creating the command! Else, the event won't fire
        Connection.AddResultForDataReader(reader => reader.NextResultCalled += (sender, args) =>
        {
            var secondReader = new DataReader(CommandBehavior.Default);
            secondReader.Add(new MyRecord {
                Name = "Milk", Amount = 3
            });
            args.Dictionary   = secondReader.Dictionary;
            args.CurrentIndex = secondReader.CurrentIndex + 1;
            args.Result       = true;
        }, new[]
        {
            new MyRecord {
                Name = "Beer", Amount = 1
            }
        });

        // Act
        using var command   = Connection.CreateCommand();
        command.CommandText = "SELECT * FROM [Fridge] WHERE Amount > 0";
        using var reader    = command.ExecuteReader();
        var result = new List <MyRecord>();

        while (reader.Read())
        {
            result.Add(new MyRecord
            {
                Name   = reader.GetString(reader.GetOrdinal("Name")),
                Amount = reader.GetInt32(reader.GetOrdinal("Amount"))
            });
        }
        if (reader.NextResult())
        {
            result.Add(new MyRecord
            {
                Name   = reader.GetString(reader.GetOrdinal("Name")),
                Amount = reader.GetInt32(reader.GetOrdinal("Amount"))
            });
        }

        // Assert
        result.Should().HaveCount(2);
        result[0].Name.Should().Be("Beer");
        result[0].Amount.Should().Be(1);
        result[1].Name.Should().Be("Milk");
        result[1].Amount.Should().Be(3);
    }