Domanda

I created a program in C# to copy a MySql database structure and procedures to a newly created database. Then upload the new data to it. The problem it fails to upload procedures that include the '@' symbol in them, example:

SET @SEARCH := CONCAT( _SEARCH , '%');

The code I use to retrieve the procedures is:

cmd = new MySqlCommand("SELECT CONCAT( 'CREATE PROCEDURE ', ISR.ROUTINE_NAME, '(', CASE WHEN ISP.PARAMETER_NAME IS NOT NULL THEN GROUP_CONCAT(CONCAT(ISP.PARAMETER_MODE, ' ', ISP.PARAMETER_NAME, ' ', ISP.DTD_IDENTIFIER)) ELSE '' END, ')\n', ISR.ROUTINE_DEFINITION) AS Create_Procedure FROM INFORMATION_SCHEMA.ROUTINES ISR LEFT JOIN INFORMATION_SCHEMA.PARAMETERS ISP ON ISR.ROUTINE_SCHEMA = ISP.SPECIFIC_SCHEMA AND ISR.ROUTINE_NAME = ISP.SPECIFIC_NAME AND ISR.ROUTINE_TYPE = ISP.ROUTINE_TYPE WHERE ROUTINE_SCHEMA = '" + Program.RemoteSource + "' GROUP BY ISR.ROUTINE_NAME;", remote);
using (MySqlDataReader dataReader = cmd.ExecuteReader())
    {
      while (dataReader.Read())
           {
             createProcedures.Add(dataReader.GetString(0));
           }
    };

and the following uploads the new create procedure statements to the new database:

counter = 0;
foreach (string procedure in createProcedures)
   {
     try
        {
            cmd = new MySqlCommand(@procedure, remote);
            cmd.ExecuteNonQuery();
            counter++;
            Console.Write("\rProcedures Created: " + counter + " of " + createProcedures.Count);
         }
     catch (MySqlException ex)
         {
             Console.WriteLine("Error: {0}", ex.ToString());
         }
    }

and the error:

Error: MySql.Data.MySqlClient.MySqlException (0x800040005): 
Fatal error encountered during command execution.  ---> 
MySql.Data.MySqlClient.MySqlException (0x800040005): 
   Parameter '@SEARCH' must be defined.

How would I go about so that MySql.Data ignores the @ symbol?

È stato utile?

Soluzione

I found the answer by adding ;Allow User Variables=True to the connection string.

Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?

Altri suggerimenti

how about using parameters, like this:

cmd = new MySqlCommand("SELECT CONCAT( 'CREATE PROCEDURE ', ISR.ROUTINE_NAME, '(', CASE WHEN ISP.PARAMETER_NAME IS NOT NULL THEN GROUP_CONCAT(CONCAT(ISP.PARAMETER_MODE, ' ',ISP.PARAMETER_NAME, ' ', ISP.DTD_IDENTIFIER)) ELSE '' END, ')\n', ISR.ROUTINE_DEFINITION) AS Create_Procedure FROM INFORMATION_SCHEMA.ROUTINES ISR LEFT JOIN INFORMATION_SCHEMA.PARAMETERS ISP ON ISR.ROUTINE_SCHEMA = ISP.SPECIFIC_SCHEMA AND ISR.ROUTINE_NAME = ISP.SPECIFIC_NAME AND ISR.ROUTINE_TYPE = ISP.ROUTINE_TYPE WHERE ROUTINE_SCHEMA = @routine_schema GROUP BY ISR.ROUTINE_NAME;", remote);
cmd.Parameters.AddWithValue("@routine_schema", Program.RemoteSource);
    using (MySqlDataReader dataReader = cmd.ExecuteReader())
        {
          while (dataReader.Read())
               {
                 createProcedures.Add(dataReader.GetString(0));
               }
        };
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top