简体   繁体   English

如果尚未创建,如何从C#以编程方式创建MySQL触发器?

[英]How to Create MySQL Trigger programmatically from C# if not already created?

I would like to create the trigger programmatically only if not created. 我只想以编程方式创建触发器(如果未创建)。

I am not able to create trigger programmatically from C# . 我无法以编程方式从C#创建触发器。 If I am executing the same trigger from MySQL Workbench 6.3 than its successfully created the trigger. 如果我从MySQL Workbench 6.3执行相同的触发器,而不是成功创建触发器。

I am receiving the error while executing from C# as below. 我从C#执行时收到错误,如下所示。

Error at line 行错误

cmd.ExecuteNonQuery(). cmd.ExecuteNonQuery()。 "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 TRIGGER Trigger_Lifetouch AFTER INSERT ON mat.lifetou' at line 1" “您的SQL语法有误;请在与MySQL服务器版本相对应的手册中找到正确的语法,以在第1行的'DELIMITER // CREATE TRIGGER Trigger_Lifetouch AFTER INSERT ON mat.lifetou'附近使用”

My Code as below 我的代码如下

public void createTrigger()
    {
            string _query = " DELIMITER // " +
                            " CREATE "+
                            " TRIGGER `Trigger_Lifetouch`  AFTER INSERT  "+
                            " ON mat.lifetouch "+
                            " FOR EACH ROW BEGIN "+
                            " INSERT INTO mat.lifetouchrespirationrate (ByDevSessionId) VALUES(2);       "+
                            " END;" ;

            using (MySql.Data.MySqlClient.MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
            {
                using (var cmd = con.CreateCommand())
                {
                    if (con.State == ConnectionState.Closed || con.State == ConnectionState.Broken)
                    {
                        con.Open();
                    }

                    int retn = 0;

                    using (var transaction = con.BeginTransaction(IsolationLevel.ReadCommitted))
                    {
                        retn = 0;
                        cmd.Transaction = transaction;
                        cmd.CommandText = _query;

                        try
                        {
                            retn = Convert.ToInt32(cmd.ExecuteNonQuery().ToString());

                            if (retn > 0)
                            {
                                logger.Debug(" Trigger Created ");
                            }
                            else
                            {
                                logger.Debug("  Trigger Not Created  ");
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Debug(ex );
                        }

                    }
                }
            }

Try to copy at runtime value from variable _query and paste it to MySql Query editor. 尝试在运行时从变量_query复制值,并将其粘贴到MySql Query编辑器中。 It's an error when you construct the query. 构造查询时出现错误。 You forgot to add at the end "DELIMITER ;" 您忘记在末尾添加“ DELIMITER”;

Trigger creation can not not be done through MySqlCommand. 触发器创建不能通过MySqlCommand完成。 you should use (new MySqlScript([ConnectionString], [TriggerScript])).Execute(); 您应该使用(new MySqlScript([ConnectionString],[TriggerScript]))。Execute();

this will create the trigger. 这将创建触发器。

try and used @ in creating a queries 尝试并在创建查询时使用@

string _query = @" DELIMITER //
                   CREATE
                   TRIGGER `Trigger_Lifetouch`  AFTER INSERT
                   ON mat.lifetouch
                   FOR EACH ROW BEGIN
                   INSERT INTO mat.lifetouchrespirationrate (ByDevSessionId) VALUES(2);       
                   END
                   //;" ;

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

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