C#、MySQL、ADO.NET、分隔符导致语法错误
我有 C# 代码,它循环遍历 .sql 文件并执行其中的内容来设置数据库。
一个 .sql
文件基本上如下:
DROP PROCEDURE IF EXISTS myProc;
DELIMITER $$
CREATE PROCEDURE myProc()
BEGIN
-- procedure stuff goes here
END $$
DELIMITER ;
CALL myProc();
当我将其输入 MySQL 查询浏览器的脚本窗口时,它完美地运行......一遍又一遍,正如人们所希望的那样。
但是,如果我将字符串放入 IDbCommand
中并执行它...
connection.Open(); // An IDbConnection
IDbTransaction transaction = connection.BeginTransaction();
using (IDbCommand cmd = connection.CreateCommand())
{
cmd.Connection = connection;
cmd.Transaction = transaction;
cmd.CommandText = line;
cmd.CommandType = CommandType.Text;
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
transaction.Rollback();
return false;
}
}
transaction.Commit();
connection.Close();
...我会得到可怕的异常 1064...
您的 SQL 语法有错误;检查手册 与您的 MySQL 服务器版本相对应,以便使用正确的语法 靠近 'DELIMITER $$ CREATE PROCEDURE myProc() BEGIN...
所以,问题是... 为什么 MySQL 让我毫无问题地执行此操作,但当我尝试从 C# 运行它时,它失败了?当然,第二个问题是我应该如何解决它。
I have C# code that cycles through .sql
files and executes what's inside them to set up a database.
One .sql
file is basically as follows:
DROP PROCEDURE IF EXISTS myProc;
DELIMITER $
CREATE PROCEDURE myProc()
BEGIN
-- procedure stuff goes here
END $
DELIMITER ;
CALL myProc();
When I input this into the MySQL Query Browser's script window, it runs perfectly... over and over again, just as one would want it to.
However, if I put the string into my IDbCommand
and execute it...
connection.Open(); // An IDbConnection
IDbTransaction transaction = connection.BeginTransaction();
using (IDbCommand cmd = connection.CreateCommand())
{
cmd.Connection = connection;
cmd.Transaction = transaction;
cmd.CommandText = line;
cmd.CommandType = CommandType.Text;
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
transaction.Rollback();
return false;
}
}
transaction.Commit();
connection.Close();
... I get the dreaded exception 1064...
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 'DELIMITER $$ CREATE PROCEDURE myProc() BEGIN...
So, the question is... why does MySQL let me do this with no problems, but when I try to run it from C#, it fails? And of course the second question is how I'm supposed to fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于那些寻找快速片段的人...
For those looking for a quick snippet...
我想你正在寻找的是这样的: "Bug #46429: use DELIMITER command in MySql .Data.MySqlClient.MySqlScript"
I think what you are looking for is this: "Bug #46429: use DELIMITER command in MySql.Data.MySqlClient.MySqlScript"
在将 SQL 发送到服务器之前,脚本中的某些命令由 mysqlk 客户端可执行解释。
请参见 mysql 命令
中所有分隔符的作用
来解释对于 C# 脚本,您必须编写知道命令是什么的代码,不能只是将命令逐行传递到服务器。
例如,您在该文件中有 3 个命令,因此只想调用 ExecuteNonQuery 3 次
,并且从 paracycle 的注释中您需要使用 MySqlScript 类
Some of the commands in your script are interpreted bt the mysqlk client excutable before the SQL is sent to the server.
See mysql commands
In effect all the delimiters
To interpret the script from C# you will have to write code that knows what the command is, you cannot just pass the commands line by line to the server.
e.g. you have 3 commands in that file and so want to call ExecuteNonQuery only 3 times
and from paracycle's coment you need to use the MySqlScript class