Saya cuba menulis data dari mysql uinlg c# ke fail .txt tetapi saya mendapat ralat "Percubaan baca tidak sah semasa pembaca ditutup.".
Kod ada di sini:
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();
Saya akan gembira jika seseorang boleh membantu saya.
Jika anda melihat kod, anda sedang mencipta
MySqlDataReader dr
并加载它,然后关闭它并创建并加载MySqlDataReader dr1
。然后,您尝试读取已关闭的dr
. Nampaknya anda juga mungkin cuba menulis kepada StreamWriter yang salah.