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(); } } } } }
作者:过错
出处:http://www.cnblogs.com/wang2650/
关于作者:net开发做的久而已。十余年时光虚度!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:wang2650@163.com
联系我,非常感谢。