错误:连接必须是有效且打开的
本文关键字:有效 连接 错误 | 更新日期: 2023-09-27 17:53:00
我得到错误
"连接必须是有效且打开的"
,我不知道是什么给了我错误,请帮助
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;
命令需要连接