Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

How to using ENUM data type in C#?


From version 9.0.0, the CUBRID database support new data type ENUM. The ENUM type is defined as the enumerated string constants. Only the specified string elements are allowed as the value of the column defined as ENUM and the maximum number of the ENUM elements is 65535. In the column of the ENUM type, each value is saved as 1 byte when the number of the ENUM elements is less than 256 and 2 bytes when the number is 256 or more. ENUM value allows numeric data type or string type.

Example

CREATE TABLE tbl (
    color ENUM('red', 'yellow', 'blue')
);

The color column can have one of following values:

Value

Index Number

NULL

NULL

'red'

1

'yellow'

2

'blue'

3

The following example shows the insertion of a value into the ENUM column.

INSERT into tbl values ('yellow'), ('red'), (2), ('blue');

The C# sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using CUBRID.Data.CUBRIDClient;

namespace Sample
{
    class ENUM_Sample
    {
        public ENUM_Sample()
        {
        }

        public void Test()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                string _connString = "server=10.34.64.122;database=demodb;port=33690;user=public;password=";

                conn.ConnectionString = _connString;
                conn.Open();

                // drop the table
                string _drop = "drop table if exists tbl;";
                Console.WriteLine(_drop);
                Console.WriteLine("");
                using (CUBRIDCommand cmd = new CUBRIDCommand(_drop, conn))
                {
                    cmd.ExecuteNonQuery();
                }

                // create a new table
                string _create = "CREATE TABLE tbl ( color ENUM('red', 'yellow', 'blue'));";
                Console.WriteLine(_create);
                Console.WriteLine("");
                using (CUBRIDCommand cmd = new CUBRIDCommand(_create, conn))
                {
                    cmd.ExecuteNonQuery();
                }

                // insert data
                string _insert = "INSERT into tbl values ('yellow'), ('red'), (2), ('blue');";
                Console.WriteLine(_insert);
                Console.WriteLine("");
                using (CUBRIDCommand cmd = new CUBRIDCommand(_insert, conn))
                {
                    cmd.ExecuteNonQuery();
                }

                // select
                string _select = "select * from tbl";
                Console.WriteLine(_select);
                Console.WriteLine("");
                using (CUBRIDCommand cmd = new CUBRIDCommand(_select, conn))
                {
                    DbDataReader _reader = cmd.ExecuteReader();
                    Console.WriteLine(_reader.GetName(0));
                    Console.WriteLine("======================");
                    while (_reader.Read())
                    {
                        Console.WriteLine(_reader.GetString(0));
                    }
                    Console.WriteLine("");
                }

                //drop
                Console.WriteLine(_drop);
                Console.WriteLine("");
                using (CUBRIDCommand cmd = new CUBRIDCommand(_drop, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}

 

ENUM_Sample _enum = new ENUM_Sample();
_enum.Test();

The program output is as follows: 

drop table if exists tbl;
 
 CREATE TABLE tbl ( color ENUM('red', 'yellow', 'blue'));
 
 INSERT into tbl values ('yellow'), ('red'), (2), ('blue');
 
 select * from tbl
 
 color
 ======================
 yellow
 red
 yellow
 blue
 drop table if exists tbl;
comments powered by Disqus
Page info
viewed 2666 times
translations en
Author
posted last year by
kevinyang
Contributors
updated last year by
View revisions
Share this article