[ad_1]
I’m tying to write to a txt file with c# some mysql columns and it shows me the error “Connection must be valid and open.”, but the connection it’s already open and dont know what to do, could you guys help me?
The code:
MySqlConnection bdcon = new MySqlConnection("Server = localhost; Database = baron; Uid = root; Pwd = database000;");
// Escreve o produto no ficheiro produto.txt
bdcon.Open();
MySqlCommand cmd_Produto = new MySqlCommand("SELECT nome from produtos");
MySqlDataReader dr = cmd_Produto.ExecuteReader();
using (StreamWriter sw = new StreamWriter(@"C:\Enzo\PAP\PAP\BarON\Ficheiros\produto.txt", true))
{
if (dr.Read())
sw.WriteLine(dr["nome"]);
}
// Escreve o produto no ficheiro produto.txt
MySqlCommand cmd_Subtotal = new MySqlCommand("SELECT subtotal from produtos_pedidos");
MySqlDataReader dr1 = cmd_Subtotal.ExecuteReader();
using (StreamWriter sw = new StreamWriter(@"C:\Enzo\PAP\PAP\BarON\Ficheiros\subtotal.txt", true))
{
if (dr1.Read())
sw.WriteLine(dr["subtotal"]);
}
// Escreve a quantidade no ficheiro quantidade.txt
//bdcon.Open();
MySqlCommand cmd_Quantidade = new MySqlCommand("SELECT quantidade from produtos_pedidos");
MySqlDataReader dr2 = cmd_Quantidade.ExecuteReader();
using (StreamWriter sw = new StreamWriter(@"C:\Enzo\PAP\PAP\BarON\Ficheiros\quantidade.txt", true))
{
if (dr1.Read())
sw.WriteLine(dr["quantidade"]);
}
// Escreve o valor no ficheiro valor.txt
MySqlCommand cmd_Valor = new MySqlCommand("SELECT valor from produtos_pedidos");
MySqlDataReader dr3 = cmd_Valor.ExecuteReader();
using (StreamWriter sw = new StreamWriter(@"C:\Enzo\PAP\PAP\BarON\Ficheiros\valor.txt", true))
{
if (dr1.Read())
sw.WriteLine(dr["quantidade"]);
}
bdcon.Close();
[ad_2]