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

问题描述

如果你需要发出查询之前打开一个SqlConnection,你能简单地处理以同样的方式的所有非公开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-

推荐答案

断开的连接状态,并需要符合之前继续使用被关闭并重新打开。

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.

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

11-02 01:19