Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

A Simple Query/Retrieve Code


Let’s take a look at a simple code which retrieves value from a CUBRID table, to get a quick insight into how coding will look like. We will assume that the connection is already opened:

String sql = "select * from nation order by `code` asc";
using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
{
    using (DbDataReader reader = cmd.ExecuteReader())
    {
        reader.Read();
        //(read the values using: reader.Get...() methods)
    }
}

Once you have a Connection open and a valid Command which provides a DbDataReader object, all you have to do is to use the Get…() methods to retrieve any column data. The CUBRID ADO.NET Driver implements all the methods required to read any CUBRID Data Types.

For example:

reader.GetString(3)
reader.GetDecimal(1)

The Get...() methods will use as an input parameter the 0-based index position of the retrieved column.

To retrieve specific CUBRID data types, you will need to use CUBRIDDataReader extension, instead of the generic DbDataReader interface:

using (CUBRIDCommand cmd = new CUBRIDCommand("select * from t", conn))
{
    CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader();

    reader.Read();
    Debug.Assert(reader.GetDateTime(0) == new DateTime(2008, 10, 31, 10, 20, 30, 040));
    Debug.Assert(reader.GetDate(0) == "2008-10-31");
    Debug.Assert(reader.GetDate(0, "yy/MM/dd") == "08/10/31");
    Debug.Assert(reader.GetTime(0) == "10:20:30");
    Debug.Assert(reader.GetTime(0, "HH") == "10");
    Debug.Assert(reader.GetTimestamp(0) == "2008-10-31 10:20:30.040");
    Debug.Assert(reader.GetTimestamp(0, "yyyy HH") == "2008 10");
}

Complete Example for this tutorial:

using System;
using CUBRID.Data.CUBRIDClient;
using System.Diagnostics;

namespace SelectExample
{
    class Program
    {
        private static readonly string connString = "server=localhost;database=demodb;port=33000;user=public;password=";

        private static void ExecuteSQL(string sql, CUBRIDConnection conn)
        {
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }

        static void Main(string[] args)
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = connString;
                conn.Open();

                ExecuteSQL("drop table if exists t", conn);
                ExecuteSQL("create table t(dt datetime)", conn);
                ExecuteSQL("insert into t values('10/31/2008 10:20:30.040')", conn);

                using (CUBRIDCommand cmd = new CUBRIDCommand("select * from t", conn))
                {
                    CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader();

                    reader.Read();
                    Debug.Assert(reader.GetDateTime(0) == new DateTime(2008, 10, 31, 10, 20, 30, 040));
                    Debug.Assert(reader.GetDate(0) == "2008-10-31");
                    Debug.Assert(reader.GetDate(0, "yy/MM/dd") == "08/10/31");
                    Debug.Assert(reader.GetTime(0) == "10:20:30");
                    Debug.Assert(reader.GetTime(0, "HH") == "10");
                    Debug.Assert(reader.GetTimestamp(0) == "2008-10-31 10:20:30.040");
                    Debug.Assert(reader.GetTimestamp(0, "yyyy HH") == "2008 10");
                }
                conn.Close();
            }
        }
    }
}

comments powered by Disqus
Page info
viewed 2370 times
translations en
Author
posted 2 years ago by
CUBRID
Contributors
updated 2 years ago by
View revisions
Share this article