我需要帮助从C#中的文件执行MySQL脚本使用MySql.Data,MySql.Data.MySqlClient

时间:2016-05-09 21:57:12

标签: c# mysql stored-procedures

代码:

private static void UpdateDatabase(string filename)
{
    string DBHost = (string)ISC.Utilities.RegistryUtils.ReadRegistryValue(Constants.REMOTE_ID_KEY, Constants.REMOTE_DB_ADDRESS_VALUE);
    string DBName = (string)ISC.Utilities.RegistryUtils.ReadRegistryValue(Constants.REMOTE_ID_KEY, Constants.REMOTE_DB_NAME_VALUE);
    string DBUID = (string)ISC.Utilities.RegistryUtils.ReadRegistryValue(Constants.REMOTE_ID_KEY, Constants.REMOTE_DB_UID_VALUE);
    string DBPWD = (string)ISC.Utilities.RegistryUtils.ReadRegistryValue(Constants.REMOTE_ID_KEY, Constants.REMOTE_DB_PWD_VALUE);
    string connectionString = string.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3}", DBHost, DBName, DBUID, DBPWD);
    MySqlConnection connection = null;

    try
    {
        string script = File.ReadAllText(filename);
        connection = new MySqlConnection(connectionString);
        connection.Open();
        MySqlCommand newcCommand = new MySqlCommand(script, connection);
        newcCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        string str = string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);
        System.Windows.Forms.MessageBox.Show(str, "Database Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
    }
    finally
    {
        if (connection != null)
        {
            connection.Close();
        }
    }
}

剧本:

DROP PROCEDURE IF EXISTS spMyProc;

CREATE PROCEDURE spMyProc()
BEGIN
  /* Do stuff */
END

CALL spMyProc();
DROP PROCEDURE spMyProc;

错误:

  

ex = {“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'CALL spMyProc()附近使用正确的语法; \ n \ n \ n \ n \ n \ n \ n \ n段处理spMyProc'在第6行” }

我不明白为什么脚本如此简单会导致这些错误。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

在您声明存储过程后,您似乎错过了分号;

END之后添加。您的代码现在应该如下所示:

DROP PROCEDURE IF EXISTS spMyProc;

CREATE PROCEDURE spMyProc()
BEGIN
  /* Do stuff */
END;

CALL spMyProc();
DROP PROCEDURE spMyProc;

希望这有帮助! :)