使用从列表视图SQL表中的所有内存的程序,如何与只有相关数据填充? [英] program using up all memory from listview sql table, how to populate with only the relevant data?

查看:106
本文介绍了使用从列表视图SQL表中的所有内存的程序,如何与只有相关数据填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我有这就是用我所有的记忆列表视图。让我解释一下我在做什么。

i think i have a listview that's using all my memory. let me explain what i'm doing

我有我需要的,我时间戳他们消息的设备,我将它们添加到sqldatabase

i have a device that i take messages from, i timestamp them, and i add them to a sqldatabase

            myConnection.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Messages (Time, Message) Values ('" + DateTime.Now + "', '"+sqlMessage+"')", myConnection);
            cmd.ExecuteNonQuery();
            myConnection.Close();
            UpdateTable();

这是我如何更新我的ListView:

here's how i update my listview:

            myConnection.Open();
            myCommand.Connection = myConnection;
            adapter.SelectCommand = myCommand;
            myCommand.CommandText = "SELECT * FROM Messages";

            DataSet ds = new DataSet();
            adapter.Fill(ds);


            lvwMessages.DataContext = ds.Tables[0].DefaultView;
            myConnection.Close();

当我离开这个运行了一段时间,我的程序通常运行的内存。我假设它的列表视图收到太多邮件从数据库中装载。但让我们说我有15条线路,我需要在屏幕上显示的消息。我怎么只加载我需要的15,然后弹出关闭那些不需要被显示出内存,并加载新的?

when i leave this running for a while, my program usually runs out of memory. i'm assuming it's the listview getting too many messages loading from the database. but let's say i have 15 lines of messages i need on the screen displayed. how do i only load the 15 that i need, then pop the ones off that don't need to be displayed out of memory, and load the new ones?

推荐答案

更改您的查询,只选择15个最近期的:

Change your query to only select the 15 most recent:

myCommand.CommandText = "SELECT TOP 15 * FROM Messages ORDER BY Time Desc";

TOP 15 表示只选择第一行15返回,而 ORDER BY 确保他们通过时间排序(即 DESC 使得它们的排序从最高(最近)到最低,抚摸最近在开始)。

TOP 15 means to select only the first 15 rows returned, and the ORDER BY makes sure they're sorted by Time (the DESC makes them sort from highest (most recent) to lowest, petting the most recent at the beginning).

(你没有提到什么DBMS,所以我贴一个在SQL Server中工作,你应该在这里未来的问题相应的数据库引擎的标记,如SQL语法和功能可以在它们之间会有所不同。)

(You didn't mention what DBMS, so I posted one that works in SQL Server. You should include the appropriate database engine tag in future questions here, as SQL syntax and functionality can vary between them.)

这篇关于使用从列表视图SQL表中的所有内存的程序,如何与只有相关数据填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆