Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

How to add multiple rows of data using CUBRID ADO.net Driver?


In the CUBRID ADO.net data provider, you can using two ways to add multiple rows of data.

The normal way is to use the SQL statement to add multiple rows of data.

INSERT INTO table_name(column_name, …) VALUES(column_value, …), VALUES(column_value, …), …

C# Sample code: 

using System.Data.Common;

using CUBRID.Data.CUBRIDClient;

namespace Sample
{
    class Add_MultipleRows
    {
        /* conection string */
        /* Please modify before using. */
        static readonly string _connString = "server=127.0.0.1;database=demodb;port=33000;user=public;password=";

        public void using_sql()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = Add_MultipleRows._connString;
                conn.Open();

                string sql = "drop table if exists table11;";
                try
                {
                    using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
                catch { }
                
                /* create new table */
                sql = "create table table11(a string , b string, c string);";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }

                /* insert multi rows values */
                sql = "insert into table11 (a, b, c) values ('1', '2','3'),('a', 'b','c'),('!', '@', '#');";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }

                /* verify count */
                sql = "select count(*) from table11";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        reader.Read();
                        if (reader.GetInt32(0) == 3)
                            ; // do something;
                    }
                }

                sql = "drop table11;";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}

The other way, you can calling CUBRIDConnection class member function :BatchExecuteNoQuery.

using System.Data.Common;

using CUBRID.Data.CUBRIDClient;

namespace Sample
{
    class Add_MultipleRows
    {
        /* conection string */
        /* Please modify before using. */
        static readonly string _connString = "server=127.0.0.1;database=demodb;port=33000;user=public;password=";

         public void using_cubrid_connection()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = Add_MultipleRows._connString;
                conn.Open();

                string[] sqls = { 
                                    "drop table if exists table11;",
                                    "create table table11(a string , b string, c string);", 
                                    "insert into table11 (a, b, c) values ('1', '2','3');",
                                    "insert into table11 (a, b, c) values ('a', 'b','c')",
                                    "insert into table11 (a, b, c) values ('!', '@', '#');"
                                };
                conn.BatchExecuteNoQuery(sqls);
                
                /* verify count */
                string sql = "select count(*) from table11";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        reader.Read();
                        if (reader.GetInt32(0) == 3)
                            ; // do something;
                    }
                }

                sql = "drop table11;";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}



comments powered by Disqus
Page info
viewed 1374 times
translations en
Author
posted last year by
kevinyang
Contributors
updated last year by
View revisions
Share this article