繁体   English   中英

错误:连接必须有效且打开

[英]Error: Connection must be valid and open

我收到错误

“连接必须有效且打开”

而且我不知道是什么给了我错误,请帮助

        string connection = @"datasource=xxx;port=xxx;username=root;password=xxx";
        MySqlConnection conn = new MySqlConnection(connection);
        MySqlCommand add = new MySqlCommand("Insert Into pap.testes (Cod_Teste,Data,Hora,Cod_Modulo,N_Processo,Nome_Teste) Values ("+ this.cod_teste.Text + "," + this.data.Text + "," + this.hora.Text + ","+ this.modulos.Text +" , " +@Entrada.PassingLoginText+ " ," + this.nome_teste.Text + " )");
        MySqlDataReader reader;

        try
        {
            conn.Open();
            reader = add.ExecuteReader();
            MessageBox.Show("Registo Inserido");
            while(reader.Read())
            {

            }

        }


        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    string connection = @"datasource=xxx;port=xxx;username=root;password=xxx";
    MySqlConnection conn = new MySqlConnection(connection);
    MySqlCommand add = conn.CreateCommand(); // important
    add.CommandText= "Insert Into pap.testes (Cod_Teste,Data,Hora,Cod_Modulo,N_Processo,Nome_Teste) Values ("+ this.cod_teste.Text + "," + this.data.Text + "," + this.hora.Text + ","+ this.modulos.Text +" , " +@Entrada.PassingLoginText+ " ," + this.nome_teste.Text + " )";
    MySqlDataReader reader;

    try
    {
        conn.Open();
        reader = add.ExecuteReader();
        MessageBox.Show("Registo Inserido");
        while(reader.Read())
        {

        }
    }

    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    finaly
    {
         conn.Close();
    }

在执行命令之前添加以下行:

add.Connection = conn;

命令需要连接,而您没有为它提供连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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