Пример #1
0
        protected void VerifyInserts(GFXDConnection conn, string cmdText,
                                     int start, int end, string addrPrefix)
        {
            // check the inserts
            GFXDCommand cmd = new GFXDCommand(cmdText, conn);
            Dictionary <int, string> result =
                new Dictionary <int, string>(end - start + 1);
            int            id;
            string         addr;
            GFXDDataReader reader = cmd.ExecuteReader();

            for (int i = start; i <= end; i++)
            {
                Assert.IsTrue(reader.Read(), "failed in read for i=" + i);
                id   = reader.GetInt32(0);
                addr = reader.GetString(1);
                Assert.IsFalse(result.ContainsKey(id),
                               "unexpected duplicate for id=" + id);
                Assert.AreEqual(addrPrefix + id, addr);
                result.Add(id, addr);
            }
            Assert.IsFalse(reader.Read());
        }
Пример #2
0
        public void DataReaderWithPositionalParameters()
        {
            // Open a new connection to the network server running on localhost
            string host    = "localhost";
            int    port    = s_clientPort;
            string connStr = string.Format("server={0}:{1}", host, port);

            using (GFXDClientConnection conn = new GFXDClientConnection(connStr)) {
                conn.Open();

                // create a table
                GFXDCommand cmd = new GFXDCommand("create table t1 (id int primary" +
                                                  " key, addr varchar(20))", conn);
                cmd.ExecuteNonQuery();

                try {
                    // insert into the table using positional parameters
                    cmd = new GFXDCommand("insert into t1 (id, addr) values (?, ?)",
                                          conn);
                    cmd.Prepare();
                    for (int i = 1; i <= s_numInserts; i++)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add(i);
                        cmd.Parameters.Add("addr" + i);

                        cmd.ExecuteNonQuery();
                    }

                    // now query the table using a DataReader
                    cmd.Parameters.Clear();
                    cmd.CommandText = "select * from t1";
                    GFXDDataReader reader     = cmd.ExecuteReader();
                    int[]          ids        = new int[s_numInserts];
                    int            numResults = 0;
                    while (reader.Read())
                    {
                        int    id   = reader.GetInt32(0);
                        string addr = reader.GetString(1);
                        if (ids[id - 1] != 0)
                        {
                            throw new Exception("Duplicate value for ID=" + id +
                                                " addr=" + addr);
                        }
                        ids[id - 1] = id;
                        numResults++;
                    }
                    reader.Close();
                    if (numResults != s_numInserts)
                    {
                        throw new Exception("unexpected number of results " + numResults);
                    }
                } finally {
                    // drop the table
                    cmd = new GFXDCommand("drop table t1", conn);
                    cmd.ExecuteNonQuery();

                    conn.Close();
                }
            }
        }