使用Close 方法关闭数据库连接,并调用 Dispose 方法释放 MySqlDataReader 和MySqlCommand 对象占用的资源。 csharp reader.Close(); connection.Close(); 完整示例代码: csharp using System; using MySql.Data.MySqlClient; class Program { static void Main() { string connectionString = "Server=localhost;Data...
时发现datareader已经isclosed==true了,以前没有遇到过.在一个函数里面reader就没有问题,但是在while里面如果会跳到另一个文件,再跳回来就close了?不知道是所有datareader的问题,还是mysql的有问题? MySqlDataReader readerProvince = DbOperate.getDataReader(sql); while (readerProvince.Read()) { int idofprovin...
(reader.Read()) { Console.WriteLine("ID: " + reader.GetInt32(0) + ", Name: " + reader.GetString(1)); } reader.Close(); } } catch (MySqlException ex) { Console.WriteLine("MySQL Error: " + ex.Message); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); ...
Hi; I have a problem where a call to DbDataReader.Close() never returns. I am just doing an ExecuteQuery and the GetSchemaTable to read the schema. The Close is in my finally. I never do a read of any row or test to see if there is a row. Any ideas? thanks - dave...
}}catch(Exceptionex){Console.WriteLine($"发生错误:{ex.Message}");}finally{// 确保连接被关闭conn.Close();}}} 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.
这样处理貌似是没问题,不过在测试中,如果“keyWords.Add(dr["KeyWord"].ToString());”出现了异常,此时,程序会调到异常处理的模块,这样,就造成了下边的close方法不会被执行到,从而造成了数据库连接数的不断累加,当达到最大值时,问题就显露出来了。
解决方法:确保在使用完 MySqlDataReader 后,调用其 Close 方法来关闭数据读取器,并释放与之关联的数据库连接。另外,也可以使用 using 语句来自动管理资源,如下所示: 代码语言:txt 复制 using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlCommand command = new ...
你已经把连接关闭了,再在外部用MySqlDataReader,当然不行了。关闭连接的时候MySqlDataReader已经没有数据了,要不dataset和datatable干吗用啊。你可以试试,把Close_Conn(Conn);注释掉,就没错了。当然这样做是不对的。解决方法:别返回MySqlDataReader,返回dataset或datatable ...
(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { return (rdr[0] + " -- " + rdr[1] + " -- " + rdr[2] + " -- " + rdr[3]); } rdr.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); Console.WriteLine(...
conn.Close();throw; } }#endregion 在这里,要注意,我们写了 一句MySqlDataReaderdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 这个的作用是"关闭SqlDataReader 会自动关闭Sqlconnection." 也就是说,关闭Sqlconnection是在关闭SqlDataReader的前提下进行,所以还是需要我们手动关闭SqlDataReader。