title: How to Create MySQL Trigger programmatically from C# if not already created?
body: <p>I would like to create the trigger programmatically only if not created. </p>
  
  <p>I am not able to create trigger programmatically from <code>C#</code>. If I am executing the same trigger from <strong>MySQL Workbench 6.3</strong> than its successfully  created the trigger. </p>
  
  <p>I am receiving the error while executing from <code>C#</code> as below. </p>
  
  <blockquote>
    <p>Error at line</p>
    
    <p>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"</p>
  </blockquote>
  
  <p>My Code as below </p>
  
  <pre><code>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 &gt; 0)
                              {
                                  logger.Debug(" Trigger Created ");
                              }
                              else
                              {
                                  logger.Debug("  Trigger Not Created  ");
                              }
                          }
                          catch (Exception ex)
                          {
                              logger.Debug(ex );
                          }
  
                      }
                  }
              }
  </code></pre>