何时会破坏与数据库的连接? [英] When the connection to the database will be destroyed?

查看:68
本文介绍了何时会破坏与数据库的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想创建一个类(名为classA),其中将建立与

数据库的连接。在另一个类(名为classB)中,

" classA"的对象。已创建并从classA的对象

的连接中检索数据。但是在C#中,我们永远不需要显式地销毁一个对象,所以

何时会断开与对象中数据库的连接?我是使用C#编写ASP.NET页面的
,我不想在检索数据后保持数据库连接。我是否需要明确销毁物品

?如果是,怎么样?


谢谢


Q.

Hi,

I want to create a class (named "classA") in which a connection to a
database will be built. In another class (named "classB"), an object of the
"classA" is created and data are retrieved from the connection of the object
of the "classA". But in C#, we never need explicitly destroy an object, so
when will the connection to the database in the object be disconnected? I am
using C# to write the ASP.NET pages and I don''t want to keep the database
connection after data have been retrieved. Do I need to destroy the object
explicitly? If yes, how?

Thanks

Q.

推荐答案

您应该在完成连接后立即关闭连接。你不需要消耗任何东西。


Quentin Huo <:Q *** @ manyworlds.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
You should close the connection as soon as you are done with it. You don''t
need to destroy anything.

"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


我想创建一个类(名为classA),其中将建立与
数据库的连接。在另一个类(名为classB)中,
的对象是classA。创建并从classA的
对象的连接中检索数据。但是在C#中,我们永远不需要显式地销毁一个对象,所以
什么时候会断开与对象中数据库的连接?我
使用C#编写ASP.NET页面,我不想在检索数据后保持数据库连接。我是否需要明确销毁对象?如果是的话,怎么样?

谢谢

Q.
Hi,

I want to create a class (named "classA") in which a connection to a
database will be built. In another class (named "classB"), an object of the "classA" is created and data are retrieved from the connection of the object of the "classA". But in C#, we never need explicitly destroy an object, so
when will the connection to the database in the object be disconnected? I am using C# to write the ASP.NET pages and I don''t want to keep the database
connection after data have been retrieved. Do I need to destroy the object
explicitly? If yes, how?

Thanks

Q.



关于连接对象,需要调用Close()或Dispose()。 (关闭是

首选。)在A类,你应该实现IDisposable并在IDisposable的Dispose方法中调用

连接'的Close方法。 ClassB

在使用classA对象完成后应调用classA.Dispose()。


搜索Dispose帮助以获取更多信息。


" Quentin Huo" <:Q *** @ manyworlds.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
On the connection object, you need to call Close() or Dispose(). (Close is
prefered.) On Class A, you should implement IDisposable and call the
connection''s Close method in the Dispose method of IDisposable. ClassB
should call classA.Dispose() when done using the classA object.

Search the help for Dispose to get more info.

"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


我想创建一个类(名为classA),其中将建立与
数据库的连接。在另一个类(名为classB)中,
的对象是classA。创建并从classA的
对象的连接中检索数据。但是在C#中,我们永远不需要显式地销毁一个对象,所以
什么时候会断开与对象中数据库的连接?我
使用C#编写ASP.NET页面,我不想在检索数据后保持数据库连接。我是否需要明确销毁对象?如果是的话,怎么样?

谢谢

Q.
Hi,

I want to create a class (named "classA") in which a connection to a
database will be built. In another class (named "classB"), an object of the "classA" is created and data are retrieved from the connection of the object of the "classA". But in C#, we never need explicitly destroy an object, so
when will the connection to the database in the object be disconnected? I am using C# to write the ASP.NET pages and I don''t want to keep the database
connection after data have been retrieved. Do I need to destroy the object
explicitly? If yes, how?

Thanks

Q.



连接会关闭或处理

连接对象时断开连接。


-

HTH,

Kevin Spencer

..Net开发商

微软MVP

大件东西组成

很多小事。


" Quentin Huo" <:Q *** @ manyworlds.com>在消息中写道

新闻:#1 ************** @ TK2MSFTNGP10.phx.gbl ...
The Connection will be disconnected when you either Close or Dispose the
Connection object.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...


我想创建一个类(名为classA),其中将建立与
数据库的连接。在另一个类(名为classB)中,
的对象是classA。创建并从classA的
对象的连接中检索数据。但是在C#中,我们永远不需要显式地销毁一个对象,所以
什么时候会断开与对象中数据库的连接?我
使用C#编写ASP.NET页面,我不想在检索数据后保持数据库连接。我是否需要明确销毁对象?如果是,怎么回事?

Q.
Hi,

I want to create a class (named "classA") in which a connection to a
database will be built. In another class (named "classB"), an object of the "classA" is created and data are retrieved from the connection of the object of the "classA". But in C#, we never need explicitly destroy an object, so
when will the connection to the database in the object be disconnected? I am using C# to write the ASP.NET pages and I don''t want to keep the database
connection after data have been retrieved. Do I need to destroy the object
explicitly? If yes, how?

Thanks

Q.



这篇关于何时会破坏与数据库的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆