如果C#中存在表格,请删除它?

JAN*_*JAN 3 c# mysql

我想在C#with MySql中删除表,只有它存在.

请考虑以下代码:

namespace CSharpMySqlSample
{
   class Example2
   {
      static void Main()
      {
         String str = @"server=localhost; database=sakila; uid=root;                password=root;";
         MySqlConnection con = null;
         try
         {
            con = new MySqlConnection(str);
            con.Open(); //open the connection        
            String cmdText = @"drop table `sakila`.`testable` if exists"; // this one drops a table 
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            cmd.Prepare();
            cmd.ExecuteNonQuery(); //execute the mysql command
         }
         catch (MySqlException err)
         {
            String outp = err.ToString();
            Console.WriteLine("Error: " + err.ToString());
         }
         finally
         {
            if (con != null)
            {
               con.Close(); //close the connection
            }
         } //remember to close the connection after accessing the database
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

它产生了:

"MySql.Data.MySqlClient.MySqlException:您的SQL语法中有错误;请检查与您的MySQL服务器版本对应的手册,以便在MySql.Data的第1行\ r \n附近使用'if exists'附近使用正确的语法.MySqlClient.MySqlStream.ReadPacket()\ r \n在MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&affectedRow,Int64&insertedId)\ r \n在MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&affectedRows,在MySql.Data.MySqlClient的MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,Boolean force)\ r \n,MySql.MySqlClient.MySqlDataReader.NextResult()\ r \n中的Int64和insertedId)\ r \n .MySqlCommand.ExecuteReader(CommandBehavior behavior)\ r \n在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()\ r \n,位于MySql.Data.MySqlClient.MySqlCommand.CSharpMySqlSample.Example2.Main()中的ExecuteNonQuery()\ r \n

那么查询有什么问题?

Kay*_*son 6

试试这个:

DROP TABLE IF EXISTS sakila.testtable;
Run Code Online (Sandbox Code Playgroud)