【问题标题】:SqlDataAdapter Close connection methodSqlDataAdapter 关闭连接方法
【发布时间】:2013-12-04 04:26:41
【问题描述】:

我的程序中有这样的代码,我相信在填充数据之后它不会关闭连接。

public static string ConnectionInfo = System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
public static DataTable ExecuteQuery(string query, string table)
    {
        SqlConnection cnn = new SqlConnection(ConnectionInfo);
        SqlDataAdapter Adp = new SqlDataAdapter(query, cnn);
        DataSet Ds = new DataSet();
        Adp.Fill(Ds, table);
        return Ds.Tables[table];
    }

这段代码有什么问题吗?

【问题讨论】:

标签: c# sql-server dataset sqldataadapter


【解决方案1】:

唯一的问题是您没有对SqlConnectionDataAdapter 使用using 语句。但是,DbDataAdapter.Fill 隐式打开和关闭连接。

public static DataTable ExecuteQuery(string query, string table)
{
    using(SqlConnection cnn = new SqlConnection(ConnectionInfo))
    using(SqlDataAdapter Adp = new SqlDataAdapter(query, cnn))
    {
        DataTable tbl = new DataTable();
        Adp.Fill(tbl);
        return tbl;
    }
}

与 SELECT 语句关联的连接对象必须是 有效,但不需要打开。如果连接关闭 在调用 Fill 之前,打开它以检索数据,然后关闭。如果 在调用 Fill 之前连接是打开的,它保持打开状态。

注意

  • using 语句将隐式关闭连接,即使发生错误
  • 我使用了DataAdapter.Fill(DataTable),因为无论如何你都在使用单个表

编辑:我刚刚注意到您正在使用表名的参数。您也可以改用DbDataAdapter.Fill(DataSet, String)。这不会改变任何事情。

【讨论】:

  • 我认为第二个using语句必须包含在第一个的括号中。
  • @AlbertoSolano:不,第二次 using 被视为单个语句,因为大括号,因此它是第一次 using 的一部分(类似于 if:if (true) if (true) {;})。跨度>
  • 嗯,我明白了,谢谢。我从未尝试过这种方法。为了更好的可读性,我总是对一行使用括号。 :-)
  • @AlbertoSolano:这是一个口味问题,但我更喜欢这种方法,因为它可以防止缩进。考虑到像SqlCommand 这样涉及更多的一次性对象,那么您已经有了三个意图,相关代码将滚出视线。 SqlConnection 初始化并不有趣。这就是为什么我总是省略大括号。
【解决方案2】:

添加 using 语句以可靠地关闭连接。这确保即使发生异常也关闭连接。更改代码如下:

public static DataTable ExecuteQuery(string query, string table)
    {
        using(SqlConnection cnn = new SqlConnection(ConnectionInfo))
        {
            SqlDataAdapter Adp = new SqlDataAdapter(query, cnn);
            DataSet Ds = new DataSet();
            Adp.Fill(Ds, table);
            return Ds.Tables[table];
        }
    }

【讨论】:

    【解决方案3】:

    无论连接的打开/关闭都应该在 try-catch-finally 块中完成。

    我们不应该使用“using” [使用 (SqlConnection connection = new SqlConnection(connectionString))] 堵塞。因为如果网络出现问题或任何异常原因。连接未关闭。所以最好使用 try-catch 块。

        public static DataTable ExecuteQuery(string query, string table)
        {
            DataSet Ds = new DataSet();
    
            SqlConnection cnn = new SqlConnection(ConnectionInfo);
    
            try{
                SqlDataAdapter Adp = new SqlDataAdapter(query, cnn);
                Adp.Fill(Ds, table);
                return Ds.Tables[table]; 
            }
            catch{
                throw;
            }
            finally{
                cnn.Close();
            }
    
        }
    

    【讨论】:

    • “我们不应该使用 using 块,因为如果网络出现问题或任何异常导致连接未关闭” 这是完全错误的,SqlConnection.Dispose 将隐式调用Close,即使出错。所以实际上一个简单的using 语句将被翻译成你的try/finally
    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多