[英]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.