ASP.NET:如何提高这几行代码的内存使用率?

发布于 2024-12-14 22:23:44 字数 964 浏览 2 评论 0原文

我有一个数据库,只有一张表,大约有 7,000 条记录。 我有一个 GridView 一次显示这些记录 10 行(我使用 GridView 提供的自动分页功能)。

这是我读取表并显示记录的方法。显然,我读了整个表,但只显示 10 行,这似乎浪费内存。但我认为我需要阅读整个 shebang 以便分页可以工作。我错了吗?如果是这样,我该如何改进此代码:

private void ShowGridViewData()
    {
        // I LEAVE ONLY RELEVANT CODE TO AVOID CLUTTER

        string queryString = (string)Session["queryString"];
        String connectionString = ConfigurationManager.ConnectionStrings["productsSQLConnectionString"].ConnectionString;
        DataSet ds = new DataSet();

        SqlConnection connection = new SqlConnection(connectionString);
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);



        // Fill the DataSet.
        adapter.Fill(ds);

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


        adapter.Dispose();
        connection.Close(); 


    }

其他信息:我无法更改代码以使用 LINQ。当我决定读取整个表时,我的另一个原因是分页不需要另一次数据库访问。我的这个推理可能是错误的。

I have a database that has only one table of about 7,000 records.
I have a GridView to display these records 10 rows at a time (I use the automatic paging feature provided with GridView).

Here's how I read in the table and show the records. Apparently, I read in the whole table but display only 10 rows which seems wasteful of memory. BUT I think I need to read in the whole shebang so that the paging can work. Am I wrong? If so how do I improve this code:

private void ShowGridViewData()
    {
        // I LEAVE ONLY RELEVANT CODE TO AVOID CLUTTER

        string queryString = (string)Session["queryString"];
        String connectionString = ConfigurationManager.ConnectionStrings["productsSQLConnectionString"].ConnectionString;
        DataSet ds = new DataSet();

        SqlConnection connection = new SqlConnection(connectionString);
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);



        // Fill the DataSet.
        adapter.Fill(ds);

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


        adapter.Dispose();
        connection.Close(); 


    }

Additional info: I cannot change the code to use LINQ. And one more reason I had when I decided to read in the whole table is that paging would not require another database access. I might be wrong with this reasoning.

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

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

发布评论

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

评论(5

再见回来 2024-12-21 22:23:44

由于您添加的评论完全改变了问题的动态,因此我将发布第二个答案。

为了避免每次都访问数据库,我会将您的数据集存储到会话中并每次从那里访问它。我已经修改了上面的代码以包含这一点,我唯一没有看到的是您在 GridView 中实现分页的位置。我认为这包含在“为了避免混乱而删除”的代码中

private void ShowGridViewData()
{
    // I LEAVE ONLY RELEVANT CODE TO AVOID CLUTTER

    DataSet ds = new DataSet();
    if (Session["ProductsDataSet") == null) {

        string queryString = (string)Session["queryString"];
        String connectionString = ConfigurationManager.ConnectionStrings["productsSQLConnectionString"].ConnectionString;

        SqlConnection connection = new SqlConnection(connectionString);
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

        // Fill the DataSet.
        adapter.Fill(ds);

        Session["ProductsDataSet"] = ds;

    } else {
        ds = (DataSet)Session["ProductsDataSet"]
    }

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


    adapter.Dispose();
    connection.Close(); 
}

Since you've added a comment that completely changes the dynamic of the question, I'm posting a second answer.

In order to keep from going to the database each and every time, I would store your dataset into Session and access it from there each time. I have modified your code from above to include this, the only thing I don't see, is where you're implementing your paging to the GridView. I assume that's included in the code that was 'removed to avoid clutter'

private void ShowGridViewData()
{
    // I LEAVE ONLY RELEVANT CODE TO AVOID CLUTTER

    DataSet ds = new DataSet();
    if (Session["ProductsDataSet") == null) {

        string queryString = (string)Session["queryString"];
        String connectionString = ConfigurationManager.ConnectionStrings["productsSQLConnectionString"].ConnectionString;

        SqlConnection connection = new SqlConnection(connectionString);
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

        // Fill the DataSet.
        adapter.Fill(ds);

        Session["ProductsDataSet"] = ds;

    } else {
        ds = (DataSet)Session["ProductsDataSet"]
    }

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


    adapter.Dispose();
    connection.Close(); 
}
享受孤独 2024-12-21 22:23:44

但我认为我需要阅读整个 shebang 以便分页可以工作。

是的,但您可以使用分页,而无需使用对象数据源加载整个数据集,请参阅当数据源未返回时手动设置 GridView 的 PageCount完整结果集?

当我决定阅读整个表格时,我的另一个原因是
该分页不需要另一个数据库访问。我可能是
这个推理是错误的。

是和否。这实际上取决于您的用例。通常,您应该更多地担心每个请求的资源利用率,而不是每个会话的资源利用率。这样,您就不会分配未使用的资源(按次付费),

因此通常我遵循按次付费模型,并且只获取我要显示的数据,除非我们正在处理

< strong>昂贵的 SQL 操作
如果您的结果集很小,但需要很长时间才能像摘要操作一样创建,我可能想像 Anthony Shaw 的答案一样缓存它。

全局状态
如果它是跨用户共享的东西,那么只有第一个用户付费,而其他人都可以免费玩。但您仍然需要一个明智的缓存策略

BUT I think I need to read in the whole shebang so that the paging can work.

Yes but you can do use paging without loading the whole dataset by using an Object Data Source see Manually setting a GridView's PageCount when DataSource doesn't return full result set?

And one more reason I had when I decided to read in the whole table is
that paging would not require another database access. I might be
wrong with this reasoning.

Yes and No. It really depends since your use case. Typically you should worry more about resource utilization per request, not per session. This way you don't allocate resources that don't get used (pay-per-play)

So usually I follow the pay-per-play model and only get the data I'm going to show unless we're dealing with

Expensive SQL operations
If you result set is small but takes a long time to create like a summary operation I might want to cache it like Anthony Shaw's answer.

Global state
If its something that shared across users then only the first user pays and the play is free for everyone else. But you still need a sensible caching policy)

小矜持 2024-12-21 22:23:44

看看这篇博客文章。它使用 SQL 中的 ROW_NUMBER() 函数创建临时 SQL 分页。您必须传递正确的页码。

http://blogs.x2line.com/al/archive/2005 /11/18/1323.aspx

额外奖励,该示例已使用 10 作为页面大小:)

Take a look at this blog entry. It creates an ad-hoc sql paging using ROW_NUMBER() function in SQL. You'll have to pass the correct page number.

http://blogs.x2line.com/al/archive/2005/11/18/1323.aspx

BONUS, the example already uses 10 as the pagesize :)

情栀口红 2024-12-21 22:23:44

一种选择是让 SQL 在存储过程中或使用 LINQ 进行分页。
这篇文章涵盖了:
实现分页的高效方法

One option is to let SQL so the paging either in your stored procedure or by using LINQ.
This post covers that:
efficient way to implement paging

眼眸 2024-12-21 22:23:44

这取决于您在做什么,但如果您的内容更新不多,请考虑缓存它。然后,当对您显示的内容进行更新时,删除/更新缓存。如果没有任何更改,这将有助于防止与 SQL 的新连接!

It depends on what you are doing but if you have content that doesn't update much consider caching it. Then when updates are made to whatever you are displaying, delete/update the cache. This will help prevent a new connection to SQL if nothing changed!

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