我是ODBC
的新手,我发现很难做我想做的简单事情。
我有一些简单的SQL
脚本,我尝试使用ODBC
使用C#
方法运行它们:
public static int ExecuteSqlNonQuery(string sql, string connectionString)
{
int result = 0;
using (var connection = new OdbcConnection(connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = sql;
result = command.ExecuteNonQuery();
connection.Close();
}
return result;
}
如果我传入的sql脚本只有一个语句,则上述方法有效,例如:
update sakila.customer set first_name = now() where customer_id = 1;
但如果它有多个语句,例如:
update sakila.customer set first_name = now() where customer_id = 1;
update sakila.customer set first_name = now() where customer_id = 2;
然后查询将失败,告诉我第一个语句后的所有内容都无效:
ERROR [42000] [MySQL][ODBC 5.3(w) Driver][mysqld-5.7.19-log]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 'update sakila.customer set first_name = now() where customer_id = 2' at line 1
那么......如何使用ODBC执行多条语句的SQL?我知道一个快速的解决方法是将字符串拆分成行并在当时运行它们,但感觉" hacky"对我来说......必须有一个"正确的"这样做的方法......对吧?
编辑 - 以防有人遇到同样的问题:
标记为重复的问题中接受的答案(说不能这样做)是不正确的。它下面的第二个未被接受的答案:
https://stackoverflow.com/a/15440056/300741
对我来说很完美。