Abhilash J A

Abhilash J A

  • 536
  • 2.4k
  • 596.5k

how to check if database exists in MySQL server?

Jan 28 2017 1:43 PM
Hello everyone,
 
 I am using WPF apps with MySQL database. I did create backup.sql file. Then, after create .exe file, how can I check if db exist then skip below code?
  1. private void Application_Startup(object sender, StartupEventArgs e)  
  2.         {  
  3.             RestoreBehavior();  
  4.         }  
 
  1. private void RestoreBehavior()  
  2.         {  
  3.             string constring = "server=localhost;user=root;pwd=123456;";  
  4.             string file = @"C:\Program Files (x86)\xxxx\xxxx\backup.sql";  
  5.             using (MySqlConnection conn = new MySqlConnection(constring))  
  6.             {  
  7.                 using (MySqlCommand cmd = new MySqlCommand())  
  8.                 {  
  9.                     using (MySqlBackup mb = new MySqlBackup(cmd))  
  10.                     {  
  11.                         cmd.Connection = conn;  
  12.                         conn.Open();  
  13.                         mb.ImportInfo.TargetDatabase = "mydb";/*Here,How can I check here if mydb exist then skip the code?*/
  14.                         mb.ImportInfo.DatabaseDefaultCharSet = "utf8";  
  15.                         mb.ImportFromFile(file);  
  16.                     }  
  17.                 }  
  18.             }  
  19.         }  

Answers (3)