垃圾收集和Visual Studio [英] Garbage Collection and Visual Studio
问题描述
我正在开发一个应用程序,我会反复使用单个连接将
记录加载到详细信息区域,为每次使用打开和关闭连接。我为
不使用VS连接控件,我使用自己的代码。
所以当相关表格关闭时,哪里是最好的地方最后
处理该连接?我假设在VS生成的覆盖中,
Dispose将是正确的位置,但我是修改该方法的犹豫,
因为它是由生成的VS.
提前致谢,
Steve Murphy
I''m working on an application where I use a single connection repeatedly to load
records into a detail area, opening and closing the connection for each use. I
do 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 finally
dispose of that connection? I''m assuming that in the VS-generated override to
Dispose would be the proper place, but I am hestitant to modify that method,
since it is generated by VS.
Thanks in advance,
Steve Murphy
推荐答案
如果你使用你自己的代码我假设你有一个管理
连接的课程?然后使用该类的Dispose方法。
如果您只是使用OLEDB连接类创建连接对象,
并重复创建连接对象,请分配连接字符串,
使用该对象,关闭对象,然后你不必担心它。
就个人而言,我喜欢使用Using构造,因为它会调用Dispose for
,即使引发了异常。非常好。
-
--- Nick Malik [微软]
MCSD,CFPS,认证Scrummaster > http://blogs.msdn.com/nickmalik
免责声明:本论坛中表达的意见是我自己的意见,而不是我雇主的b $ b代表。
我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。
-
Steve Murphy < SM ***** @ granite-ridge.com>在消息中写道
news:e6 ****************** @ TK2MSFTNGP14.phx.gbl ...
If you "use your own code" I assume that you have a class that manages the
connection? then use the Dispose method of that class.
If you are just creating a connection object using OLEDB Connection classes,
and you repeatedly create a connection object, assign the connection string,
use the object, and close the object, then you don''t have to worry about it.
Personally, I like using the Using construct, since it calls Dispose for
you, even if an exception is raised. Pretty nice.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Steve Murphy" <sm*****@granite-ridge.com> wrote in message
news:e6******************@TK2MSFTNGP14.phx.gbl...
I我在一个应用程序上工作,我多次使用一个连接
将记录加载到一个详细信息区域,为每个
使用打开和关闭连接。我没有使用VS连接控件,我使用自己的代码。
因此,当关联的表单关闭时,最终处理该连接的最佳位置在哪里?我假设在VS生成的覆盖中,
到Dispose将是正确的位置,但我很想修改那个
方法,因为它是由VS生成的。
提前致谢,史蒂夫墨菲
I''m working on an application where I use a single connection repeatedly to load records into a detail area, opening and closing the connection for each use. I do 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 finally
dispose of that connection? I''m assuming that in the VS-generated override to Dispose would be the proper place, but I am hestitant to modify that method, since it is generated by VS.
Thanks in advance,
Steve Murphy
Steve Murphy< sm ***** @ granite-ridge。 COM>写道:
Steve Murphy <sm*****@granite-ridge.com> wrote:
我正在开发一个应用程序,我会反复使用单个连接将记录加载到详细信息区域,为每次使用打开和关闭连接。我没有使用VS连接控件,我使用自己的代码。
因此,当关联的表单关闭时,最终处理的最佳位置在哪里?那连接?我假设在VS生成的覆盖中,
Dispose将是正确的位置,但我是修改该方法的犹豫,因为它是由VS生成的。
I''m working on an application where I use a single connection repeatedly to load
records into a detail area, opening and closing the connection for each use. I
do 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 finally
dispose of that connection? I''m assuming that in the VS-generated override to
Dispose would be the proper place, but I am hestitant to modify that method,
since it is generated by VS.
听起来你好像不必要地挂在连接上了。
就个人而言,我建议创建一个新的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 time
you 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 form
itself, so the lifetime issue doesn''t come up.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
>如果您使用自己的代码我假设你有一个管理
> If you "use your own code" I assume that you have a class that manages the
连接的类?
还没有。我是C#和.NET的新手。这个应用程序更像是一个概念证明,
我已经复制了我在旧平台上使用的外观和感觉。我计划将
将所有数据访问代码移动到数据层中的单独类中,但我还没有获得
。我的第一步是让自己满意,我可以用我在旧平台上做的C#.NET做同样的事情,并且
一样相对容易。
然后使用该类的Dispose方法。
是的,但我会在哪里处理类对象?记住,我想重新使用
相同的连接对象,直到卸载表单。我应该以'Dispose方法的形式处理
连接吗?如果是这样,当我修改该方法时,是否有来自
Visual Studio的问题?
如果您只是使用OLEDB连接创建连接对象类,
和你反复创建一个连接对象,分配连接字符串,使用该对象,并关闭该对象,然后你不必担心它。
我在''load事件表单中创建一次。然后我根据需要打开并关闭它。
就个人而言,我喜欢使用Using构造,因为即使引发异常,它也会调用Dispose for
。非常好。
connection?
Not yet. I''m new to C# and .NET. This application is more of a proof of concept,
where I''ve duplicated the look and feel I used on my old platform. I plan to
move all data access code into separate classes in a data layer, but I haven''t
gotten there yet. My first step was to satisfy myself that I could do the same
kinds of things with C#.NET that I used to do on my old platform, and with the
same relative ease.
then use the Dispose method of that class.
Yes, but where would I dispose of the class object? Remember, I want to resuse
the same connection object until the form is unloaded. Should I dispose of the
connection in the form''s Dispose method? If so, are there any gotcha''s from
Visual Studio when I modify that method?
If you are just creating a connection object using OLEDB Connection classes,
and you repeatedly create a connection object, assign the connection string,
use the object, and close the object, then you don''t have to worry about it.
I create it once in the form''s load event. Then I open and close it, as needed.
Personally, I like using the Using construct, since it calls Dispose for
you, even if an exception is raised. Pretty nice.
是的,我只是在Visual C#.NET 2003
Unleashed中阅读了这种方法。霍夫曼和克鲁格的书。这很不错。但这是否适用
这里,因为我不想处理连接对象,直到表格是
卸载?
谢谢你的回复,
Steve Murphy
Yes, I was just reading about that approach in the "Visual C#.NET 2003
Unleashed" book by Hoffman and Kruger. It is pretty nice. But does that apply
here, since I don''t want to dispose of the connection object until the form is
unloaded?
Thank you for the reply,
Steve Murphy
这篇关于垃圾收集和Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!