'C# WPF connecting to MySql server and create db if doesn't exist
I'm trying to create new DB if it doesn't exist when pressing the button "Restore" but it keeps jumping to the exception. What am i doing wrong?
private void Button_Click_Restore(object sender, RoutedEventArgs e)
{
string CreateDatabase;
string dbInfo = "DATABASE=" + Database.Text;
string connectionString = "SERVER=" + Hostname.Text + ";" + dbInfo + ";" + "UID=" + User.Text + ";" + "PASSWORD=" + Password.Text + ";" + "PORT=" + Port.Text;
MySqlConnection connection = new MySqlConnection(connectionString);
CreateDatabase = "CREATE DATABASE IF NOT EXISTS " + Database.Text + " ; ";
MySqlCommand command = new MySqlCommand(CreateDatabase);
try
{
connection.Open();
command.ExecuteReader();
MessageBox.Show("DB has been created.");
}
catch (Exception)
{
MessageBox.Show("Please Check Server and Database name.");
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
