我收到错误
"连接必须有效并且打开"
我不知道是什么给了我这个错误,请帮忙
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);
}
答案 0 :(得分:1)
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();
}
答案 1 :(得分:0)
在执行命令之前添加此行:
add.Connection = conn;
命令需要连接 而且你没有为你提供一个。