显式关闭 DataSet 的底层连接?

发布于 2024-09-08 15:51:29 字数 167 浏览 3 评论 0原文

我正在使用 DataSet 从 Microsoft SQL Server 检索数据。我是否需要显式关闭连接(或者底层的SqlDataAdapter自动关闭连接)?

我总是使用 DataReader(using),但第一次使用 DataSet —— 这就是为什么想知道最佳实践。提前致谢。

I am using DataSet to retrieve data from the Microsoft SQL Server. Do I need to explicitly close the connection (or the underlying SqlDataAdapter automatically closes the connection)?

I always use DataReader (with using), but first time using DataSet -- that's why wondering about best practice. Thanks in advance.

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

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

发布评论

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

评论(5

树深时见影 2024-09-15 15:51:29

DataSet 是数据库上断开连接的“视图”。
也就是说,您将数据库中的数据加载到 DataSet 中(实际上是在 DataTable 中,可以将其放入 DataSet 中),您可以关闭用于填充 DataTableDataSet 的连接。

您可以继续使用数据集中的数据。它不需要与数据库的开放连接。

事实上,一旦您不需要任何数据库访问,就应该立即关闭数据库连接。与数据库的连接应该是短暂的。

A DataSet is a disconnected 'view' on the database.
That is, you load the data from the database in a DataSet (actually, in a DataTable, which can be put in a DataSet), and you can close the Connection that you've used to populate the DataTable or DataSet.

You can continue to work with the data that is in the dataset. It does not require an open connection to the DB.

In fact, you should close a DB-connection as soon as you don't need any DB access soon. Connections to databases should be short-lived.

海螺姑娘 2024-09-15 15:51:29

最佳实践是为所有实现 IDisposable 的 ADO.NET 成员调用 Dispose():连接、命令、适配器、表、集、读取器等:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
    connection.Open();
    using (DataSet ds = new DataSet())
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        adapter.Fill(ds);
    }
}

The best practice is to call Dispose() for all ADO.NET members implemented IDisposable: connection, command, adapter, table, set, reader, etc:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
    connection.Open();
    using (DataSet ds = new DataSet())
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        adapter.Fill(ds);
    }
}
单挑你×的.吻 2024-09-15 15:51:29

using 语句在对象被垃圾收集之前清理非托管资源。连接是非托管资源,因此即使您使用数据集,它也应该关闭。

Using statement clean up unmanaged resources before the object is garbage collected. The connection, is an unmanaged resources so it should be close even if your are with a DataSet.

姐不稀罕 2024-09-15 15:51:29

我始终认为跟踪我的连接是一个好主意,无论我以哪种方式连接到数据库。

你说你总是使用数据读取器,但现在你使用的是数据集。我假设这意味着您正在使用 DataAdapter 来配合您的 DataSet。如果是这种情况,并且您使用的是 MSSQL,那么 SqlDataAdapter 将为您打开和关闭连接,但就像我说的,我喜欢自己跟踪这一点,特别是因为您可能会使用SqlCommand.ExecuteScalar(即使您大多数时间使用 DataAdapter),并且 SqlCommand 不会管理您的连接状态为你。

SqlDataAdapter 文档:
http://msdn.microsoft.com/en-us/library/zxkb3c3d。 ASPX

I always think it is a good idea to keep track of my connections, no matter wich way I'm connecting to a database.

You said that you always use a datareader, but now you are using a data set. I'm assuming that means you are using a DataAdapter to go with your DataSet. If that is the case, and you are using MSSQL, then the SqlDataAdapter will open and close the connection for you, but like I said, I like to keep track of this myself, especially since you may use SqlCommand.ExecuteScalar (even if you are using a DataAdapter most of the time) at some point, and the SqlCommand will not manage your connection state for you.

SqlDataAdapter doc:
http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx

世界和平 2024-09-15 15:51:29

只是为了让事情变得清楚,我遵循传统的初学者与数据库交互的方式。

public DataSet GetData()
{
    SqlDataReader reader;
    string connstr = your conn string;
    SqlConnection conn = new SqlConnection(connstr);
    DataTable st = new DataTable();
    DataSet ds = new DataSet();
    try
    {                   
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Your select query";
        cmd.Connection = conn;
        conn.Open();

        reader = cmd.ExecuteReader();
        dt.Load(reader);
        ds.Tables.Add(dt);
    }
    catch (Exception ex)
    {
        // your exception handling 
    }
    finally
    {
        reader.Close();
        reader.Dispose();
        conn.Close();
        conn.Dispose();
    }    
    return ds;
}

Just for making things clear i am following conventional beginners way of interacting with db.

public DataSet GetData()
{
    SqlDataReader reader;
    string connstr = your conn string;
    SqlConnection conn = new SqlConnection(connstr);
    DataTable st = new DataTable();
    DataSet ds = new DataSet();
    try
    {                   
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Your select query";
        cmd.Connection = conn;
        conn.Open();

        reader = cmd.ExecuteReader();
        dt.Load(reader);
        ds.Tables.Add(dt);
    }
    catch (Exception ex)
    {
        // your exception handling 
    }
    finally
    {
        reader.Close();
        reader.Dispose();
        conn.Close();
        conn.Dispose();
    }    
    return ds;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文