예제 #1
0
파일: CopyTests.cs 프로젝트: sealong/npgsql
        public void BinaryRoundtrip()
        {
            ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
            var longString = new StringBuilder(Conn.BufferSize + 50).Append('a').ToString();

            using (var writer = Conn.BeginBinaryImport("COPY data (field_text, field_int2) FROM STDIN BINARY"))
            {
                StateAssertions(Conn);

                writer.StartRow();
                writer.Write("Hello");
                writer.Write(8, NpgsqlDbType.Smallint);

                writer.WriteRow("Something", (short)9);

                writer.StartRow();
                writer.Write(longString);
                writer.WriteNull();
            }

            using (var reader = Conn.BeginBinaryExport("COPY data (field_text, field_int2) TO STDIN BINARY"))
            {
                StateAssertions(Conn);

                Assert.That(reader.StartRow(), Is.EqualTo(2));
                Assert.That(reader.Read <string>(), Is.EqualTo("Hello"));
                Assert.That(reader.Read <int>(NpgsqlDbType.Smallint), Is.EqualTo(8));

                Assert.That(reader.StartRow(), Is.EqualTo(2));
                Assert.That(reader.IsNull, Is.False);
                Assert.That(reader.Read <string>(), Is.EqualTo("Something"));
                reader.Skip();

                Assert.That(reader.StartRow(), Is.EqualTo(2));
                Assert.That(reader.Read <string>(), Is.EqualTo(longString));
                Assert.That(reader.IsNull, Is.True);
                reader.Skip();

                Assert.That(reader.StartRow(), Is.EqualTo(-1));
            }
        }