I'm trying to write data from mysql uinlg c# to a .txt file but I'm getting the error "Invalid read attempt while reader is closed.".
Code here:
StreamWriter sr_Produto = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\produto.txt", true); bdcon.Open(); MySqlCommand cmd = new MySqlCommand("SELECT nome from produtos",bdcon); MySqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { sr_Produto.Write(dr["nome"].ToString()); } bdcon.Close(); bdcon.Open(); StreamWriter sr_Quantidade = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\quantidade.txt", true); MySqlCommand cmd1 = new MySqlCommand("SELECT quantidade from produtos_pedidos",bdcon); MySqlDataReader dr1 = cmd1.ExecuteReader(); if (dr.Read()) { sr_Produto.Write(dr["quantidade"].ToString()); } bdcon.Close(); bdcon.Open(); StreamWriter sr_Subtotal = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\subtotal.txt", true); MySqlCommand cmd2 = new MySqlCommand("SELECT subtotal from produtos_pedidos", bdcon); MySqlDataReader dr2 = cmd2.ExecuteReader(); if (dr.Read()) { sr_Produto.Write(dr["subtotal"].ToString()); } bdcon.Close(); bdcon.Open(); StreamWriter sr_Valor = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\valor.txt", true); MySqlCommand cmd3 = new MySqlCommand("SELECT valor from produtos"); MySqlDataReader dr3 = cmd3.ExecuteReader(); if (dr.Read()) { sr_Produto.Write(dr["valor"].ToString()); } bdcon.Close();
I would be happy if anyone could help me.
P粉1210816582024-04-01 09:00:25
If you look at the code, you are creating MySqlDataReader dr
and loading it, then closing it and creating and loading MySqlDataReader dr1
. Then you try to read the closed dr
. It also looks like you may be trying to write to the wrong StreamWriter.