本文介绍了垃圾收集和Visual Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在开发一个应用程序,我会反复使用单个连接将 记录加载到详细信息区域,为每次使用打开和关闭连接。我为不使用VS连接控件,我使用自己的代码。 所以当相关表格关闭时,哪里是最好的地方最后 处理该连接?我假设在VS生成的覆盖中, Dispose将是正确的位置,但我是修改该方法的犹豫, 因为它是由生成的VS. 提前致谢, Steve Murphy I''m working on an application where I use a single connection repeatedly to loadrecords into a detail area, opening and closing the connection for each use. Ido not use a VS connection control for this, I use my own code. So when the associated form is closed, where is the best place to finallydispose of that connection? I''m assuming that in the VS-generated override toDispose would be the proper place, but I am hestitant to modify that method,since it is generated by VS. Thanks in advance,Steve Murphy推荐答案 听起来你好像不必要地挂在连接上了。 就个人而言,我建议创建一个新的SqlConnection(或其他) 并打开它然后每次 你需要它来处理它(使用using语句)。让连接池管理数据库连接 - 这就是它的用途。 这样你就不需要连接了形式 本身,所以终生问题不会出现。 - Jon Skeet - < ; sk *** @ pobox.com> http://www.pobox .com / ~siget 如果回复该组,请不要给我发邮件 It sounds to me like you''re needlessly hanging onto the connection.Personally, I''d recommend creating a new SqlConnection (or whatever)and opening it then disposing it (with a using statement) every timeyou need it. Let the connection pool manage the database connection -that''s what it''s there for. That way you need never have a connection associated with the formitself, so the lifetime issue doesn''t come up. --Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too 是的,我只是在Visual C#.NET 2003 Unleashed中阅读了这种方法。霍夫曼和克鲁格的书。这很不错。但这是否适用 这里,因为我不想处理连接对象,直到表格是 卸载? 谢谢你的回复, Steve Murphy Yes, I was just reading about that approach in the "Visual C#.NET 2003Unleashed" book by Hoffman and Kruger. It is pretty nice. But does that applyhere, since I don''t want to dispose of the connection object until the form isunloaded?Thank you for the reply,Steve Murphy 这篇关于垃圾收集和Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 07:02