错误消息是,
连接必须有效且打开。
这个密码错了吗?我怎样才能解决这个问题?

string strConn = "server = localhost; user = root; database = ****; port = 3306; password = ****; Charset = utf8";
using (MySqlConnection conn = new MySqlConnection(strConn))
{
    MySqlCommand insertCommand = new MySqlCommand();

    conn.Open();

    for (int i = 0; i < 10; i++)
    {
        insertCommand.CommandText = "INSERT INTO master (col_name, col_code)" +
        " SELECT * from (select '" + _name[i] + "', '" + _code[i] + "') as tmp" +
        " WHERE NOT EXISTS (" +
        " SELECT col_code FROM master WHERE col_code = '" + _name[i] + "') limit 1;";

        insertCommand.ExecuteNonQuery();
    }
    conn.Close();
}

最佳答案

在需要为CommandObject设置连接之前,例如:

InsertCommand.Connection = conn;

关于c# - C#MySQL查询无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39263937/

10-12 02:47