在打开SqlConnection之前处理不同的ConnectionState [英] Handling different ConnectionStates before opening SqlConnection

查看:1035
本文介绍了在打开SqlConnection之前处理不同的ConnectionState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您需要在发出查询之前打开一个SqlConnection,您能以同样的方式处理所有非Open ConnectionStates吗?例如:

If you need to open a SqlConnection before issuing queries, can you simply handle all non-Open ConnectionStates in the same way? For example:

    if (connection.State != ConnectionState.Open)
    {
        connection.Open();
    }

我在某处读到ConnectionState.Broken的连接需要在重新打开。有没有人有这方面的经验?感谢 -

I read somewhere that for ConnectionState.Broken the connection needs to be closed before its re-opened. Does anyone have experience with this? Thanks-

推荐答案

http://msdn.microsoft.com/en-us/library/system.data.connectionstate.aspx

断开的连接状态确实需要关闭并重新打开,才能继续使用。

Broken connection state does need to be closed and reopened before eligible for continued use.

编辑:不幸的是,关闭一个关闭的连接也会受阻。在对未知连接执行操作之前,您需要测试ConnectionState。也许一个短暂的switch语句可以做的伎俩。

Unfortunately closing a closed connection will balk as well. You'll need to test the ConnectionState before acting on an unknown connection. Perhaps a short switch statement could do the trick.

这篇关于在打开SqlConnection之前处理不同的ConnectionState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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