【问题标题】:SqlConnection.Close() inside using statementSqlConnection.Close() 在 using 语句中
【发布时间】:2013-09-06 10:30:58
【问题描述】:

我正在使用此代码:

    public void InsertMember(Member member)
    {
        string INSERT = "INSERT INTO Members (Name, Surname, EntryDate) VALUES (@Name, @Surname, @EntryDate)";

        using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
        {
            sqlConnection.Open();

            using (SqlCommand sqlCommand = new SqlCommand(INSERT, sqlConnection))
            {
                sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = member.Name;
                sqlCommand.Parameters.Add("@Surname", SqlDbType.VarChar).Value = member.Surname;
                sqlCommand.Parameters.Add("@EntryDate", SqlDbType.Date).Value = member.EntryDate;

                sqlCommand.ExecuteNonQuery();
            }
        }
    }

如果我在处理之前不添加sqlConnection.Close(); 会不会出错?我是说。它没有显示任何错误,完全没有问题。最好先关闭它吗?如果是,为什么?

【问题讨论】:

  • 即使出现异常,using语句也会Dispose连接,所以你真的不需要Close调用

标签: c# sql .net sql-server database


【解决方案1】:

无需Close or Dispose using 块会为您处理这些问题。

MSDN所述:

以下示例创建一个 SqlConnection,打开它,显示一些 的属性。最后自动关闭连接 使用块。

private static void OpenSqlConnection(string connectionString) 
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    } 
}

【讨论】:

  • 是关于Close(),而不是Dispose() :)
  • 代码有connection.Close();就在你的倒数第二个花括号'}'之前?当 using 块尝试关闭已被代码关闭的连接时,这会引发异常吗?
【解决方案2】:

using statement ensures that Dispose is called 即使在您调用对象上的方法时发生异常。 You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block;事实上,这就是 using 语句被编译器翻译的方式。 MSDN

所以最终你的代码行

  using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))

会被编译器调用IDisposable object in the finally转换成普通的try finally block

【讨论】:

    【解决方案3】:

    MSDN documentation for the Close method

    您必须通过调用 CloseDispose 显式关闭连接。 CloseDispose 在功能上是等效的。

    因此,调用Dispose(隐含地,甚至使用using)将覆盖你的基地。

    同样值得注意的是,我认为,虽然不是特定于您的情况,但当事物被包装在 using 语句中时,总是会有效地调用 Close - 这可能不会如果没有适当的try/catch/finally 处理就会出现异常。

    【讨论】:

    • 在示例中它指出:以下示例创建一个 SqlConnection,打开它,显示它的一些属性。连接在 using 块结束时自动关闭。
    • @DarrenDavies 准确地说。它在细节上比这更明确。
    【解决方案4】:

    不加sqlConnection.Close();是不是错了在丢弃之前

    不,只要您在Using 中使用您的连接,它就不是。当您离开使用范围时,将调用Dispose 进行sql 连接。这将关闭现有连接并释放所有资源。

    【讨论】:

      【解决方案5】:

      using 语句是一个 try finally 块,在您的情况下,最终块将有一个 connection.Dispose() 调用。所以你真的不需要一个独立的connection.Close() 声明。

      这样做的好处是即使在出现异常的情况下也能确保处置,因为 finally 块将始终运行。

      try
      {
      sqlConnection.Open();
      // ....
      }
      finally
      {
      if(sqlConnection != null)
            sqlConnection.Dispose();
      }
      

      【讨论】:

        【解决方案6】:

        您正在使用Using,它将为您提供Dispose() 对象。

        如果您在 Using 语句之外建立连接,那么是的 - 您需要在完成后关闭连接。

        【讨论】:

          【解决方案7】:

          不,没有错。 sqlConnection 在通过 using 块并调用 Dispose 方法后将关闭连接。 SqlConnection.Dispose() 等于 SqlConnection.Close() 方法。

          来自 MSDN:如果 SqlConnection 超出范围,它不会被关闭。因此,您必须通过调用 Close 或 Dispose 显式关闭连接。 Close 和 Dispose 在功能上是等效的。

          【讨论】:

            猜你喜欢
            • 2019-02-05
            • 2013-12-01
            • 2010-11-22
            • 1970-01-01
            • 2018-11-18
            • 2011-03-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多