简体   繁体   English

使用C#Application运行MySQL DDL命令

[英]Run MySQL DDL command using C# Application

I am using MySQL database with C# to develop an application.Using MySQL Server 5.0 and odbc connector. 我正在使用带有C#的MySQL数据库来开发一个应用程序。使用MySQL Server 5.0和odbc连接器。 In some cases I am required to execute ddl commands such as ALTER TABLE or CREATE TABLE to manipulate the database. 在某些情况下,我需要执行ddl命令,例如ALTER TABLECREATE TABLE来操作数据库。 In these cases I need to use the IF EXISTS command to check the database before I execute commands. 在这些情况下,我需要使用IF EXISTS命令在执行命令之前检查数据库。 I write below commands that execute without any problem in Navicat or Workbench, but do not work when send this commands with application by ExecuteNoneQury methods. 我在下面编写的命令在Navicat或Workbench中没有任何问题地执行,但是当通过ExecuteNoneQury方法使用应用程序发送此命令时不起作用。

what is wrong? 怎么了?

use db;
drop procedure if exists  sp_update ;
delimiter //

create procedure sp_update()
begin

     if not exists( SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tab' AND COLUMN_NAME = 'col' and table_schema = 'db') then
       ALTER TABLE `tab` ADD COLUMN `col`  int(11) NULL DEFAULT NULL ;
    end if;

end//

delimiter ;
call sp_update();
drop procedure if exists  sp_update ;

C# Command : C#命令:

public override int ExecuteNoneQuery(string commandText)
    {
        int obTemp = 0;
        Conn = new MySqlConnection(Connection.ConnectionString);
        try
        {
            MySqlCommand MySqlCommand = new MySqlCommand(commandText, Conn);
            if (Conn.State == ConnectionState.Closed)
            {
                Conn.Open();
            }
            obTemp = MySqlCommand.ExecuteNonQuery();
        }
        finally
        {
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }
        }
        return obTemp;
    }

"delimiter" is not MySQL syntax.. It is a convinience function for the mysql command line client and is only understood by it (well, some GUI clients mimic the behavior too, to be able to run scripts that are originally thought for command line client). “delimiter”不是MySQL语法..它是mysql命令行客户端的一个令人信服的功能,只有它才能被理解(好吧,一些GUI客户端也模仿了这个行为,能够运行原本被认为是命令行的脚本)客户)。

But, you do not need "delimiter" in any code executed by connectors. 但是,在连接器执行的任何代码中都不需要“分隔符”。 Using it will result in syntax error like the one you got. 使用它将导致语法错误,就像你得到的那样。

I solved my own problem. 我解决了自己的问题。 I needed to split up my sql command into two parts. 我需要将我的sql命令分成两部分。

Part 1 create procedure: 第1部分创建过程:

drop procedure if exists  sp_update ;
create procedure sp_update()
begin

     if not exists( SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tab' AND COLUMN_NAME = 'col' and table_schema = 'db') then
       ALTER TABLE `tab` ADD COLUMN `col`  int(11) NULL DEFAULT NULL ;
    end if;
end

Part 2: 第2部分:

call sp_update();
drop procedure if exists  sp_update ;

Send each command to MySQL separately. 将每个命令分别发送给MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM