Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C MySQL查询不工作_C#_Mysql - Fatal编程技术网

C# C MySQL查询不工作

C# C MySQL查询不工作,c#,mysql,C#,Mysql,错误消息是 连接必须有效且打开 这个代码错了吗?我怎样才能解决这个问题 string strConn = "server = localhost; user = root; database = ****; port = 3306; password = ****; Charset = utf8"; using (MySqlConnection conn = new MySqlConnection(strConn)) { MySqlCommand insertCommand = new

错误消息是

连接必须有效且打开

这个代码错了吗?我怎样才能解决这个问题

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();
}
在for循环之前,需要为CommandObject设置连接,如:

InsertCommand.Connection = conn;

您需要使用您创建的conn连接对象将连接指定给您的命令

因此,您的代码如下所示:

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

    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();  //you don't need this.
}

你指向的代码在哪里?你能在这里插入你的代码吗?我添加了图像,但无法加载。什么是无法加载?不要发布图像,而是发布你的代码。使用MySqlconnection conn=new MysqlConnectionstrconnIt wors!谢谢:@lovendon如果它有效,那么适当的回答将是投票并将其标记为已回答:
string strConn = "server = localhost; user = root; database = ****; port = 3306; password = ****; Charset = utf8";
using (MySqlConnection conn = new MySqlConnection(strConn))
{
    MySqlCommand insertCommand = conn.CreateCommand();

    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();  //you don't need this.
}
using (MySqlConnection conn = new MySqlConnection(strConn))
{
    //your code
}