使用Connector / NET编程创建数据库?

时间:2010-03-28 10:22:45

标签: c# mysql database

如何使用连接器/网络编程创建数据库?为什么以下不起作用?

    string connStr = "server=localhost;user=root;port=3306;password=mysql;";
    MySqlConnection conn = new MySqlConnection(connStr);
    MySqlCommand cmd;
    string s0;

    try
    {
        conn.Open();
        s0 = "CREATE DATABASE IF NOT EXISTS `hello`;";
        cmd = new MySqlCommand(s0, conn);
        conn.Close();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }

4 个答案:

答案 0 :(得分:16)

您需要执行命令并确保正确处理对象:

string connStr = "server=localhost;user=root;port=3306;password=mysql;";
using (var conn = new MySqlConnection(connStr))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = "CREATE DATABASE IF NOT EXISTS `hello`;";
    cmd.ExecuteNonQuery();
}

答案 1 :(得分:13)

您可能想要执行MySqlCommand。现在您只需创建一个,但它不会执行您的查询。

试试这个:

conn.Open();
s0 = "CREATE DATABASE IF NOT EXISTS `hello`;";
cmd = new MySqlCommand(s0, conn);
cmd.ExecuteNonQuery();
conn.Close();

答案 2 :(得分:1)

执行您的命令并尝试此功能

public void createDatabase(string server, string port, string database, string username, string password)
    {
        string connectionstring = string.Format("Server = {0}; Port ={1}; Uid = {2}; Pwd = {3}; pooling = true; Allow Zero Datetime = False; Min Pool Size = 0; Max Pool Size = 200; ", server, port, username, password);
        using (var con = new MySqlConnection { ConnectionString = connectionstring })
        {
            using (var command = new MySqlCommand { Connection = con })
            {
                if (con.State == ConnectionState.Open)
                    con.Close();

                try
                {
                    con.Open();
                }
                catch (MySqlException ex)
                {
                    msgErr(ex.Message + " connection error.");
                    return;
                }

                try
                {
                    command.CommandText = @"CREATE DATABASE IF NOT EXISTS @database";
                    command.Parameters.AddWithValue("@database", database);
                    command.ExecuteNonQuery();//Execute your command
                }
                catch (MySqlException ex)
                {
                    msgErr(ex.Message + " sql query error.");
                    return;
                }
            }
        }

    }

答案 3 :(得分:-1)

导入MySql.Data.MySqlClient 公共类Form1     私有dBf As String =" Empresa"     Private str As String =" Server = localhost; UID =根; PWD =;数据库= MySQL的;池=假;"     Private Cmd As MySqlCommand     Private Con As New MySqlConnection(str)     Private Sub Form1_Load()处理MyBase.Load         尝试             Con.Open()             Cmd =新的MySqlCommand("创建数据库,如果不存在" + dBf,Con)             Cmd.ExecuteNonQuery():Con.ChangeDatabase(dBf)             Cmd = Con.CreateCommand             Cmd.CommandText =" CREATE TABLE Empleado" &安培;              "(Id Char(10)COLLATE utf8_spanish_ci Not Null,"&              "名称VarChar(50)COLLATE utf8_spanish_ci Not Null," &安培;              "电子邮件VarChar(50)COLLATE utf8_spanish_ci Not Null," &安培;              "网站VarChar(50)COLLATE utf8_spanish_ci Not Null," &安培;              "主键(id))ENGINE = MyISAM"             Cmd.ExecuteNonQuery()             MsgBox(" Registros,ok !!!"):Con.Close()         赶上前例外             MSGBOX(Err.Description)         结束尝试     结束子 结束班