发生异常时关闭数据库连接的正确方法

发布于 2024-08-29 19:57:50 字数 842 浏览 3 评论 0原文

如果出现异常,以下代码是否使连接保持打开状态?

我正在使用 Microsoft SQL 精简版数据库。

try
{
    SqlCeConnection conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
        new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
      // do some stuff
    }

    conn.Close();
}
catch (Exception ex)
{
    ExceptionManager.HandleException(ex);
}

当然更好的方法是在 try 之前声明一个连接对象,在 try 块内建立连接并在 finally 块中关闭它?

 SqlCeConnection conn = null;
 try
 {
    conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
        new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
      // do some stuff
    }
}
catch (Exception ex)
{
    ExceptionManager.HandleException(ex);
}
finally
{
    if( conn != null )  conn.Close();
}

Does the following code leave the connection open if there is an exception?

I am using a Microsoft SQL compact edition database.

try
{
    SqlCeConnection conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
        new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
      // do some stuff
    }

    conn.Close();
}
catch (Exception ex)
{
    ExceptionManager.HandleException(ex);
}

Surely a better way would be to declare a connection object before the try, establish a connection inside the try block and close it in a finally block?

 SqlCeConnection conn = null;
 try
 {
    conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
        new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
      // do some stuff
    }
}
catch (Exception ex)
{
    ExceptionManager.HandleException(ex);
}
finally
{
    if( conn != null )  conn.Close();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

扮仙女 2024-09-05 19:57:50

您在 using 块的帮助下在代码中处理 SqlCeCommand 的方式,您可以对 SqlCeConnection 执行相同的操作。

SqlCeConnection conn;
using (conn = new SqlCeConnection(ConnectionString))
{
   conn.Open();
   using (SqlCeCommand cmd = 
       new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
   {
   // do some stuff
   }
}

注意:您可以对实现 IDisposable 的类使用 using 块。

编辑:这与

try
{
    conn = new SqlCeConnection(ConnectionString);
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "...";

    cmd.ExecuteNonQuery();
}
finally
{
    conn.Close();
}

参考相同: http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection%28VS.80%29.aspx

The way you are handling SqlCeCommand in your code with the help of a using block, you could do the same for the SqlCeConnection.

SqlCeConnection conn;
using (conn = new SqlCeConnection(ConnectionString))
{
   conn.Open();
   using (SqlCeCommand cmd = 
       new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
   {
   // do some stuff
   }
}

Note: You can use a using block for classes that implement IDisposable.

EDIT: This is same as

try
{
    conn = new SqlCeConnection(ConnectionString);
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "...";

    cmd.ExecuteNonQuery();
}
finally
{
    conn.Close();
}

ref: http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection%28VS.80%29.aspx

娇俏 2024-09-05 19:57:50

使用使用

using(SqlConnection conn = new SqlConnection())
{
//put all your code here.
}

Use Using

using(SqlConnection conn = new SqlConnection())
{
//put all your code here.
}
澜川若宁 2024-09-05 19:57:50
try
catch
finally

是处理此问题的正确方法,因为连接应始终在最后关闭。但您不仅应该检查 conn != null,还应该检查 conn 状态是否不是 Closed

try
catch
finally

is the proper way to handle this, because connection should always be closed at the end. but you should check not only that conn != null, but also if conn state is not Closed.

岁月蹉跎了容颜 2024-09-05 19:57:50

您应该使用 using 语句,它可以轻松处理连接关闭
http://davidhayden.com/blog/dave/archive/ 2005/01/13/773.aspx

You should use using statement, which handles the connection closing without hassles
http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx

简美 2024-09-05 19:57:50

你必须尝试以下方式。
因为连接在 Final 块中关闭

try
{
    SqlCeConnection conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
        new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
      // do some stuff
    }
}
catch (Exception ex)
{

}
finally
{
\\close connection here
}

You Have To Try Following Way.
Because Connection Close In Finally Block

try
{
    SqlCeConnection conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
        new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
      // do some stuff
    }
}
catch (Exception ex)
{

}
finally
{
\\close connection here
}
小猫一只 2024-09-05 19:57:50

为什么不在连接周围使用 using 以及 SqlCeCommand

Why not use a using around the connection as well as the SqlCeCommand ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文