C#、MySQL、ADO.NET、分隔符导致语法错误

发布于 2024-08-02 13:19:53 字数 1126 浏览 15 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

围归者 2024-08-09 13:19:53

对于那些寻找快速片段的人...

var connectionString = @"server=ChangeMe;user=ChangeMe;pwd=ChangeMe;database=ChangeMe;"; 

var scriptText = File.ReadAllText(@"C:\script.sql");

using (var connection = new MySqlConnection(connectionString))
{
    var script = new MySqlScript(connection, scriptText);
    connection.Open();
    script.Execute();
}  

For those looking for a quick snippet...

var connectionString = @"server=ChangeMe;user=ChangeMe;pwd=ChangeMe;database=ChangeMe;"; 

var scriptText = File.ReadAllText(@"C:\script.sql");

using (var connection = new MySqlConnection(connectionString))
{
    var script = new MySqlScript(connection, scriptText);
    connection.Open();
    script.Execute();
}  
七婞 2024-08-09 13:19:53

在将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文