Multiple insert statements in one ODBC ExecuteNonQuery (C #)

I insert several rows into the database and combine them together to improve performance. I get an ODBCException saying that my SQL syntax is incorrect. But when I try to use it in mysql command line client, it works fine. I conducted simplified testing 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 in the same database:


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}

+5
source share
3 answers

Yes, ODBC does NOT support batch processing.

But there is another option:

  • Use the MySQL.NET Connector instead of ODBC.
  • INSERT MySQL: INSERT INTO test(id, name) VALUES ('1', 'Foo'), ('2', 'bar');.
+5

MySQL ODBC v5 +, "" ODBC ( Windows) " ".

OPTIONS = 67108864 odbc.

: http://dev.mysql.com/doc/refman/5.0/en/connector-odbc-configuration-connection-parameters.html

+8

It cannot handle batch processing (using multiple statements to separate), since this requires two-way communication. I am afraid that you need to do this in a loop and go into the database several times.

In fact, I could never use batch processing with any managed provider.

+1
source

All Articles