Search code examples
c#mysqlsmo

Is there a way of creating a database in MYSQL without using SMO in C#


I'm trying to create a database through C# without using SMO, is it possible?! I've got this at the moment but I'm 99% it's wrong;

connectionString2 = "SERVER=" + server + ";" + "DATABASE=master;" 
                            + "UID=" + uid + ";" + "PASSWORD=" + password + ";"

this.OpenConnection(); //Which opens the above connection

// The Lines array has stored the sql statements to create the databse 
// and tables for that database 
foreach (string line in lines)
{
    create_database += line;
}
MySqlCommand cmd = new MySqlCommand(create_database, connection2);
cmd.ExecuteNonQuery();

is it actually possible do this without using SMO?!


Solution

  • You have to use MySqlCommand as you're doing in the example.

    MySqlCommand cmd = new MySqlCommand("CREATE DATABASE YAYNEWDATABASE;", connection2);
    connection2.Open();
    cmd.ExecuteNonQuery();
    connection2.Close();
    

    Alternatively if you're going to be doing a lot of modification I'd suggest trying a tool like Manatee to handle your database creation/migration as part of a build script. This is making a lot of assumptions about what you're doing, but it doesn't hurt to be aware of it.

    This adds migrations similar to those in rails to .net.

    {
        up: "CREATE TABLE Orders (ID {pk}, OrderNumber {string} NOT NULL, SubTotal {money})",
        down: "DROP TABLE Orders"
    }