コード例 #1
0
        public void DataReader_GetNumericTypes_Test()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = DBHelper.connString;
                conn.Open();

                DBHelper.ExecuteSQL("drop table if exists t", conn);
                DBHelper.ExecuteSQL("create table t(c0 short, c1 smallint, c2 integer, c3 int, c4 bigint, c5 numeric, c6 decimal, c7 float, c8 real, c9 double, c10 double precision, c11 monetary)", conn);
                DBHelper.ExecuteSQL("insert into t values(-10, 11, -32768, 32769, -2147483650, 2.5, -12.6, 33.5, -123.4567, 23.45, 45.678, 987.65)", conn);
                DBHelper.ExecuteSQL("insert into t values(-11, 12, -32769, 32770, -2147483651, 2.6, -12.7, 34.5, -223.4567, 33.45, 46.678, 989.65)", conn);

                String sql = "select * from t";

                LogTestStep("use exactly the same data type");
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    using (CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader())
                    {
                        reader.Read(); //retrieve just one row

                        Assert.AreEqual(12, reader.FieldCount);
                        Assert.AreEqual(-10, reader.GetInt16(0));
                        Assert.AreEqual(11, reader.GetInt16(1));
                        Assert.AreEqual(-32768, reader.GetInt32(2));
                        Assert.AreEqual(32769, reader.GetInt32(3));
                        Assert.AreEqual(-2147483650, reader.GetInt64(4));
                        //Assert.AreEqual(2.5, reader.GetDecimal(5)); //Actual 3
                        //Assert.AreEqual(-12.6, reader.GetDecimal(6)); //Actual -13
                        Assert.AreEqual(33.5, reader.GetFloat(7));
                        if (Math.Abs(-123.4567 - reader.GetFloat(8)) > 0.00001)
                        {
                            Assert.Fail();
                        }

                        Assert.AreEqual(23.45, reader.GetDouble(9));
                        Assert.AreEqual(45.678, reader.GetDouble(10));
                        //TODO: ask whether there is GetMONETARY method
                        Assert.AreEqual(987.65, reader.GetDouble(11));

                        LogStepPass();
                    }
                }

                //TODO change the datatype
                //LogTestStep("use exactly the same data type");
            }

            LogTestResult();
        }
コード例 #2
0
        private static void TestGetData()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                ExecuteSQL("drop table if exists t", conn);

                string sql = "create table t(";
                sql += "c_integer_ai integer AUTO_INCREMENT, ";
                sql += "c_smallint smallint, ";
                sql += "c_integer integer, ";
                sql += "c_bigint bigint, ";
                sql += "c_numeric numeric(15,1), ";
                sql += "c_float float, ";
                sql += "c_decimal decimal(15,3), ";
                sql += "c_double double, ";
                sql += "c_char char(1), ";
                sql += "c_varchar character varying(4096), ";
                sql += "c_time time, ";
                sql += "c_date date, ";
                sql += "c_timestamp timestamp, ";
                sql += "c_datetime datetime, ";
                sql += "c_bit bit(32), ";
                sql += "c_varbit bit varying(4096), ";
                sql += "c_monetary monetary, ";
                sql += "c_string string";
                sql += ")";
                ExecuteSQL(sql, conn);

                sql  = "insert into t values(";
                sql += "1, ";
                sql += "11, ";
                sql += "111, ";
                sql += "1111, ";
                sql += "1.1, ";
                sql += "1.11, ";
                sql += "1.111, ";
                sql += "1.1111, ";
                sql += "'a', ";
                sql += "'abcdfghijk', ";
                sql += "TIME '13:15:45 pm', ";
                sql += "DATE '00-10-31', ";
                sql += "TIMESTAMP '13:15:45 10/31/2008', ";
                sql += "DATETIME '13:15:45 10/31/2008', ";
                sql += "B'0110011110101001', ";
                sql += "B'010110110111110000', ";
                sql += "123456789, ";
                sql += "'qwerty'";
                sql += ")";
                ExecuteSQL(sql, conn);

                sql = "select * from t";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader();
                    Thread.CurrentThread.CurrentCulture   = CultureInfo.CreateSpecificCulture("en-US");
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                    while (reader.Read()) //only one row will be available
                    {
                        int Ordinal = reader.GetOrdinal("c_integer_ai");
                        Debug.Assert(Ordinal == 0);

                        try
                        {
                            Guid guid = reader.GetGuid(0);
                        }
                        catch (Exception e)
                        {
                            string error = e.ToString();
                            Debug.Assert(e.Message == "Value does not fall within the expected range.");
                        }

                        try
                        {
                            Guid guid = reader.GetGuid(9);
                        }
                        catch (Exception e)
                        {
                            string error = e.ToString();
                            Debug.Assert(e.Message == "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).");
                        }

                        int  i = reader.GetInt32(0);
                        Type t = reader.GetColumnType(0);
                        Debug.Assert(i == 1);

                        short s = reader.GetInt16(1);
                        t = reader.GetColumnType(1);
                        Debug.Assert(s == 11);

                        i = reader.GetInt32(2);
                        t = reader.GetColumnType(2);
                        Debug.Assert(i == 111);

                        long i64 = reader.GetInt64(3);
                        t = reader.GetColumnType(3);
                        Debug.Assert(i64 == 1111);

                        decimal d = reader.GetDecimal(4);
                        t = reader.GetColumnType(4);
                        Debug.Assert(d == (decimal)1.1);

                        float f = reader.GetFloat(5);
                        t = reader.GetColumnType(5);
                        Debug.Assert(f == (float)1.11);

                        d = reader.GetDecimal(6);
                        t = reader.GetColumnType(6);
                        Debug.Assert(d == (decimal)1.111);

                        double db = reader.GetDouble(7);
                        t = reader.GetColumnType(7);
                        Debug.Assert(db == (double)1.1111);

                        char c = reader.GetChar(8);
                        t = reader.GetColumnType(8);
                        Debug.Assert(c == 'a');

                        string str = reader.GetString(9);
                        t = reader.GetColumnType(9);
                        Debug.Assert(str == "abcdfghijk");

                        DateTime dt = reader.GetDateTime(10);
                        t = reader.GetColumnType(10);
                        Debug.Assert(dt.Second == 45);

                        dt = reader.GetDateTime(11);
                        t  = reader.GetColumnType(11);
                        string dt_s = reader.GetDate(11);
                        Debug.Assert(dt == new DateTime(2000, 10, 31));

                        dt   = reader.GetDateTime(12);
                        t    = reader.GetColumnType(12);
                        dt_s = reader.GetDate(12, "yyyy-MM-dd");
                        dt_s = reader.GetTimestamp(12);
                        dt_s = reader.GetTimestamp(12, "yyyy-MM-dd HH:mm:ss.fff");
                        Debug.Assert(dt == new DateTime(2008, 10, 31, 13, 15, 45));

                        dt   = reader.GetDateTime(13);
                        t    = reader.GetColumnType(13);
                        dt_s = reader.GetTime(13);
                        dt_s = reader.GetTime(13, "HH:mm:ss");

                        Debug.Assert(dt == new DateTime(2008, 10, 31, 13, 15, 45));

                        byte[] by       = new byte[4];
                        byte[] expected = { 0x67, 0xa9, 0x00, 0x00 };
                        reader.GetBytes(14, 0, by, 0, 4);
                        Debug.Assert(by.SequenceEqual(expected));
                        t = reader.GetColumnType(14);

                        expected[0] = 0x5b; expected[1] = 0x7c; expected[2] = 0x00;
                        reader.GetBytes(15, 0, by, 0, 3);
                        Debug.Assert(by.SequenceEqual(expected));
                        t = reader.GetColumnType(15);

                        Debug.Assert(reader.GetString(16) == "123456789.0000000000000000");
                        t = reader.GetColumnType(16);
                        char[] buffer = new char[16];
                        reader.GetChars(16, 0, buffer, 0, 16);
                        Debug.Assert(reader.GetString(17) == "qwerty");
                        t = reader.GetColumnType(17);
                    }
                }

                ExecuteSQL("drop table t", conn);
            }
        }