简体   繁体   中英

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?!

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"
}

You might use something like this:

var connection = new MySqlConnection("Data Source=server;UserId=root;PWD=pass;");
var command = new MySqlCommand("CREATE DATABASE FancyDatabase;", connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();

Note: there is no default database, and you need to have the rights. And: this is untested code, uses standard sql features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM