CUBRID学习笔记 36 在net中添加多行记录

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();
                }
            }
        }
    }
}

  上面的代码是正常的体位.   

      下面的是批量提交.主要使用了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();
                }
            }
        }
    }
}

  

posted @ 2016-03-17 16:49  过错  阅读(195)  评论(0编辑  收藏  举报