本文介绍了数据库连接问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我有几个问题,我一直想问。这些都是 都与ADO.net有关,特别是与数据库连接。 1)如果我通过Connection.open打开了与数据库的连接() 方法,我不使用Connection.close()方法,将垃圾收集器 收集连接对象只是因为我不再使用它了。 2)我正在使用数据集,其中我对数据进行了一些修改,并且 将修改后的数据提交回服务器。是否会再次向数据库服务器打开连接 ,因为数据集是一个无连接对象。 3)当我说connection.close()时,是连接实际上是 关闭或连接信息是否保留在某个内存区域,只有 等待其他一些connection.open()方法来调用它。 提前感谢 pradeep TP 解决方案 连接关闭,连接对象被声明为globaly(或 ,实例是一个dataadapter,它有一个引用它)比对象 留在内存中。 顺便说一句,这是新闻组的典型问题。 Microsoft.public.dotnet.framework.adonet 我希望这会有所帮助, Cor 是否引用意味着打开的连接对象保持未闭合状态。我b $ b的意思是,如果在我打开连接对象并且没有关闭 长之后,GC会认为这是未引用的并尝试收集 对象。 " Mark R. Dawson"写道: 垃圾收集器只会这样做,如果你这样做没有任何参考您的程序中的任何其他地方的连接。 GC也是非确定性的,因此在GC运行之前,连接可能会打开很长时间。最好将你的连接用法放在一个using语句中,它将自动调用dispose(内部调用 close)或者总是使用finally语句即 使用(SqlConnection) connection = new SqlConnection()) { ...... } SqlConnection connection = null; 尝试 {连接=新的SqlConnection(); .... } 终于 { if(connection!= null) { connection.Close(); } } 当您在连接上调用Close或Dispose将基础连接返回到连接池时,连接池组连接基于连接字符串一起使用,所以下次尝试使用相同的连接字符串打开连接时,您将在池中获得连接(如果有可用的话)很快没有性能损失。 Mark R Dawson http:/ /www.markdawson.org " pradeep_TP"写道: Hi all,I have a few questions that I have been wanting to ask for long. These areall related to ADO.net and specifically to conenction to database.1) If I have opened a connection to a database through Connection.open()method, and I do not use Connection.close() method, will garbage collectorcollect the connection object just because i am not using it any more.2) I am using a dataset, in which i make some modifications to the data andsubmit the modified data back to the server. Will the connection be openedagain to the database server, because dataset is a connectionless object.3) When i m saying connection.close(), is the connection actually gettingclose or does the connection information stays in some memory area, onlywaiting for some other connection.open() method to call it.thanks in advancepradeep T.P 解决方案The connection closes and when the connection object is declared globaly (orby instance a dataadapter which has a reference to it) than the objectstays in memory.By the way, this are typical questions for the newsgroup.Microsoft.public.dotnet.framework.adonetI hope this helps,CorDoes "references" mean opened connection object remaining unclosed. What imean is that if after i open the connection object and does not close it forlong, will the GC consider this as unreferenced and try to collect theobject."Mark R. Dawson" wrote: The garbage collector will only do this if you do not have any references to the connection anywhere else in your program. Also the GC is non-deterministic so the connection may lie open for a long time before the GC runs. It is best to put your connection useage inside a using statement that will automatically call dispose automatically (which internally calls close) or always use a finally statement i.e. using(SqlConnection connection = new SqlConnection()) { ...... } or SqlConnection connection = null; try { connection = new SqlConnection(); .... } finally { if(connection != null) { connection.Close(); } } When you call Close on a connection or Dispose the underlying connection is returned to the connection pool, the connection pool group connections together based on the Connection String, so the next time you try to open a connection with the same connectionstring you will get a connection in the pool (if there is one available) very quickly without a performance hit. Mark R Dawson http://www.markdawson.org "pradeep_TP" wrote: 这篇关于数据库连接问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-07 09:54