单ODBC的ExecuteNonQuery(C#)多重插入语句 [英] Multiple insert statements in single ODBC ExecuteNonQuery (C#)
本文介绍了单ODBC的ExecuteNonQuery(C#)多重插入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我插入多行成一个数据块,并在试图改善性能一起连接它们。
我得到一个ODBCException告诉我,我的SQL语法是错误的。但是,当我尝试在mysql命令行客户端,它工作得很好..
我跑了简化的测试来形容这个过程。
命令行客户端:
MySQL的创建表测试(`id` INT,`name`文字);
查询确定,受影响的0行(0.05秒)MySQL的> INSERT INTO测试(ID,姓名)VALUES('1','富'); INSERT INTO测试(ID,姓名)VALUES('2','棒');
查询行,1行受影响(0.00秒)查询行,1行受影响(0.00秒)MySQL的>
在我跑在同一个DB这code:
comm.CommandText =INSERT INTO测试(ID,姓名)VALUES('1','富'); INSERT INTO测试(ID,姓名)VALUES('2','棒');;
comm.ExecuteNonQuery();
这给了我以下错误:
+基地{ERROR [42000] [MySQL的] [ODBC 5.1驱动]的[mysqld-51年5月1日社区]你在你的SQL语法错误;检查对应于您的MySQL服务器版本的使用权语法手册附近的INSERT INTO测试(ID,姓名)VALUES('2','棒')第1行} {System.Data.Common.DbException} System.Data.Odbc.OdbcException
解决方案
是,ODBC不支持批量处理。
但还有另一种选择:
- 使用 MySQL的.NET连接器的,而不是ODBC。
- 然后使用MySQL替代的INSERT语句:
INSERT INTO测试(ID,姓名)VALUES('1','富'),('2','棒');
。
I'm inserting multiple rows into a DB, and joining them together in an attempt to improve performance. I get an ODBCException telling me my SQL syntax is wrong. But when I try it in the mysql commandline client, it works just fine.. I ran a simplified test to describe the process.
Command Line Client:
mysql> create table test (`id` int, `name` text);
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO test(id, name) VALUES ('1', 'Foo');INSERT INTO test(id, name) VALUES ('2', 'bar');
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql>
After that I ran this code on the same DB:
comm.CommandText = "INSERT INTO test(id, name) VALUES ('1', 'Foo');INSERT INTO test(id, name) VALUES ('2', 'bar');";
comm.ExecuteNonQuery();
which gives me the following error:
+ base {"ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.51-community]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO test(id, name) VALUES ('2', 'bar')' at line 1"} System.Data.Common.DbException {System.Data.Odbc.OdbcException}
解决方案
Yes, ODBC does NOT support batch processing.
But there is another option:
- Use the MySQL .NET Connector instead of ODBC.
- Then use the MySQL alternative INSERT statement:
INSERT INTO test(id, name) VALUES ('1', 'Foo'), ('2', 'bar');
.
这篇关于单ODBC的ExecuteNonQuery(C#)多重插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文