C# 在打开 SqlConnection 之前处理不同的 ConnectionStates

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/103532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:07:04  来源:igfitidea点击:

Handling different ConnectionStates before opening SqlConnection

提问by wnka

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

如果您需要在发出查询之前打开一个 SqlConnection,您是否可以简单地以相同的方式处理所有非打开的 ConnectionStates?例如:

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

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

我在某处读到 ConnectionState.Broken 连接需要在重新打开之前关闭。有任何人对此有经验吗?谢谢-

采纳答案by ddc0660

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

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.

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

Edit: 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.

编辑:不幸的是,关闭关闭的连接也会犹豫不决。在处理未知连接之前,您需要测试 ConnectionState。也许简短的 switch 语句可以解决问题。

回答by Joe

This isn't directly answering your question, but the best practice is to open and close a connection for every access to the database. ADO.NET connection pooling ensures that this performs well. It's particularly important to do this in server apps (e.g. ASP.NET), but I would do it even in a WinForms app that accesses the database directly.

这不是直接回答您的问题,但最佳做法是为每次访问数据库打开和关闭连接。ADO.NET 连接池可确保其性能良好。在服务器应用程序(例如 ASP.NET)中执行此操作特别重要,但我什至会在直接访问数据库的 WinForms 应用程序中执行此操作。

Example:

例子:

using(SqlConnection connection = new SqlConnection(...))
{
   connection.Open();
   // ... do your stuff here

}  // Connection is disposed and closed here, even if an exception is thrown

In this way you never need to check the connection state when opening a connection.

通过这种方式,您在打开连接时永远不需要检查连接状态。

回答by Rudy Hinojosa

You can handle it the same way. I was getting numerous connection state == broken while using IE9. There is something fundamentally wrong with IE9 in this regard since no other browser had this issue of broken connection states after 5 or 6 updates to the database tables. So I use object context. So basically just close it and re-open it.

你可以用同样的方式处理它。我在使用 IE9 时遇到了很多连接状态 == 中断。在这方面,IE9 存在一些根本性的错误,因为在对数据库表进行 5 或 6 次更新后,没有其他浏览器出现连接状态断开的问题。所以我使用对象上下文。所以基本上只需关闭它并重新打开它。

I have this code before all my reads and updates in the businss logic layer:

我在业务逻辑层中的所有读取和更新之前都有此代码:

if (context.Connection.State == System.Data.ConnectionState.Broken)
{
    context.Connection.Close();
    context.Connection.Open();
}