关闭数据库连接...非常快速的问题 [英] Closing Database Connections ... very quick question

查看:65
本文介绍了关闭数据库连接...非常快速的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照主题行,这是一个非常快速的问题...

我在程序的各个位置都有类似的代码:

As per the Subject line, a very quick question ...

I have code similar to this in various places in my program :

If mySqlConn.State = ConnectionState.Closed Then mySqlConn.Open()
            Using mySqlRdr As SqlCeDataReader = mySqlCmd.ExecuteReader()
                mySqlCmd.ExecuteNonQuery()
                mySqlConn.Close()
                Me.Hide()
                MasterForm.Show()
            End Using



我刚刚注意到,尽管我总是在某些地方打开 mySqlConn ...,但我还是关闭了 MySqlConn &在某些地方,我关闭了 mySqlRdr ...我应该做什么,还是应该同时做这两个事情?!?



I''ve just noticed that although I always Open mySqlConn ... in some places I close MySqlConn & in some places I close mySqlRdr ... which should I be doing, or should I be doing both ?!?

推荐答案

上面的代码,同时关闭Connection(显式地通过teh Close方法)和Readewr(即通过using块执行关闭并处理)的Readewr.

您应该做的是同时关闭这两个容器,并且(最好)同时处置这两个容器,因为它们是稀缺资源,不应被占用!
就个人而言,我可以在Connection,Command和Reader上使用using块来确定.
In the code above, you close both your Connection (explicitly, via teh Close method) and the Readewr (implitly, via the using block which does a Close and Dispose).

What you should be doing is closing both and (preferably) Disposing both, since they are scarce resources and should not be hogged!
Personally, I use a using block on the Connection, Command and Reader to be sure.


这篇关于关闭数据库连接...非常快速的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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