ASP.NET:适配器填充数据集后是否应该立即关闭连接?

发布于 2024-11-15 07:13:38 字数 214 浏览 5 评论 0原文

我正在使用 GridView,想知道在语句之后立即关闭连接是否对我更好

adapter.Fill(ds);

,或者我应该等到完成:

GridView.DataSource = ds;
GridView.DataBind();

我假设一旦数据集被填充,我就不再需要连接。我错了吗?

I am using a GridView and wonder if it's better for me to close the connection IMMEDIATELY after the

adapter.Fill(ds);

statement or I should wait until I've done:

GridView.DataSource = ds;
GridView.DataBind();

I assume once the dataset has been filled, I no longer need the connection. Am I wrong?

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

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

发布评论

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

评论(4

街道布景 2024-11-22 07:13:38

当您调用 adapter.Fill(ds); 时,数据将加载到内存中,您可以在执行此语句后立即关闭连接。之后,当您将 ds 设置为 gridview 的 DataSource 时,它​​将绑定内存中的数据。

请阅读本文以了解使用断开连接的数据 - DataSet 和 SqlDataAdapter

When you call adapter.Fill(ds); the data is loaded into memory and you can close the connection immediately after this statement. After, when you set ds as DataSource to the gridview, it will bind the data from memory.

Have a look into this article for understanding Working with Disconnected Data - The DataSet and SqlDataAdapter

少年亿悲伤 2024-11-22 07:13:38

调用 Fill() 后,您可以安全地关闭连接。题外话:使用 使用块在处理 IDisposable (如 SqlConnection)实体时,因此,在这种情况下,您不必手动调用关闭()

After call to Fill(), you can safely close the connection. Offtopic: make it a practice using using block when dealing with IDisposable (like SqlConnection) entities so, in this case, you wouldn't have to manually call Close().

另类 2024-11-22 07:13:38

将数据填充到 DataSet 后,SqlDataAdapter 不需要连接,但如果您在此之后进行任何数据操作您需要再次打开它,我所做的是将所有此类语句写入 finally 块。

SqlDataAdapter doesn't need connection after filling data into DataSet, but if you are doing any data manipulation after that line you will need to open it again, what i do is i write all such statements in finally block.

白云不回头 2024-11-22 07:13:38

根本不需要打开 SqlConnection,因为 DataSet 具有断开连接的体系结构。

DataSet是主要的数据存储
ADO.NET 中的工具已断开连接
建筑学。与数据读取器不同,
DataSet 未直接连接
通过连接到数据库
填充对象时。反而,
从数据库填充数据集
首先创建一个DataAdapter对象
(例如 SqlDataAdapter)
提供者并将其与
SqlConnection 对象。然后
SqlDataAdapter 可以代理数据
通过发出以下命令来检索数据集
针对数据库的 SqlCommand
通过SqlConnection,检索
数据,并填充数据集

一个非常基本的使用示例将是

string sSQL = "SELECT * FROM Products";
string sConnString = 
    "Server=(local);Database=Northwind;Integrated Security=SSPI;";
SqlDataAdapter oDa = new SqlDataAdapter();
DataSet oDs = new DataSet();
using(SqlConnection oCn = new SqlConnection(sConnString))
{
    SqlCommand oSelCmd = new SqlCommand(sSQL, oCn);
    oSelCmd.CommandType = CommandType.Text;
    oDa.SelectCommand = oSelCmd;
    oDa.Fill(oDs, "Products");
}

来源:对比ADO.NET DataReader 和 DataSet

There is no need to open the SqlConnection at all as DataSet has a disconnected architecture.

The DataSet is the main data storage
tool in the ADO.NET disconnected
architecture. Unlike the DataReader,
the DataSet is not connected directly
to a database through a Connection
object when you populate it. Instead,
to fill a DataSet from a database you
first create a DataAdapter object
(such as a SqlDataAdapter) for the
provider and associate it with a
SqlConnection object. Then the
SqlDataAdapter can broker the data
retrieval for the DataSet by issuing a
SqlCommand against the database
through the SqlConnection, retrieving
the data, and filling the DataSet

A very basic example of usage will be

string sSQL = "SELECT * FROM Products";
string sConnString = 
    "Server=(local);Database=Northwind;Integrated Security=SSPI;";
SqlDataAdapter oDa = new SqlDataAdapter();
DataSet oDs = new DataSet();
using(SqlConnection oCn = new SqlConnection(sConnString))
{
    SqlCommand oSelCmd = new SqlCommand(sSQL, oCn);
    oSelCmd.CommandType = CommandType.Text;
    oDa.SelectCommand = oSelCmd;
    oDa.Fill(oDs, "Products");
}

Source: Contrasting the ADO.NET DataReader and DataSet

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