开口的SqlConnection已已经关闭 [英] Opening SqlConnection that has been closed already

查看:248
本文介绍了开口的SqlConnection已已经关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有与关闭和打开相同的SqlConnection对象,而不是创造新的每次什么问题? 例如:

Are there any problems with closing and opening of the same SqlConnection object instead of creating new one each time? For example:

SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
//Some work here
conn.Close()
//Some work here... conn stays in scope
conn.Open()

有没有可能通过打开连接第二次获得非法状态异常?

Is there any chance to get illegal state exception by opening connection second time?

推荐答案

您可以重新打开关闭它(你不能重新打开已释放连接,虽然)后的连接。

You can reopen the connection after closing it (you cannot reopen a disposed connection, though).

至于戴维齐赫的问题 - 我们的一些客户有每个连接许可证在他们的数据库,关闭连接允许其他应用程序使用它。

As for Dave Zych's question - some of our customers have per-connection licences on their databases, closing a connection allows other applications to use it.

通常你必须连接池启用,在这种情况下,数据库的实际连接将不会(总是)被关闭(立即),但可以在应用程序中使用的其他相等的连接对象。

Usually you'll have connection pooling enabled, in which case the actual connection to the database will not (always) be closed (immediately) but can be used by other, equal connection objects within your application.

这篇关于开口的SqlConnection已已经关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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