存储在应用与选择查询如何读取一个DataTable的数据? [英] how to read data from a DataTable that stored in application with select query?

查看:203
本文介绍了存储在应用与选择查询如何读取一个DataTable的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯开发我的网站搜索模块。为了让它工作得更快,我想我的产品表存储在应用。像这样的:

 数据层DL =新数据层();
        字符串CS = dl.dataLayerConnectionString;
        System.Data.SqlClient.SqlConnection CON =新System.Data.SqlClient.SqlConnection(CS);        字符串FetchSqlSyntax =SELECT AllImages.ImgSrc,Product.FName,Product.EName,Product.PID FROM AllImages INNER JOIN产品。AllImages.PID = Product.PID;        System.Data.SqlClient.SqlCommand FetchCmd =新System.Data.SqlClient.SqlCommand(FetchSqlSyntax,CON);        System.Data.SqlClient.SqlDataAdapter DA =新System.Data.SqlClient.SqlDataAdapter(FetchCmd);        System.Data.DataTable DT =新System.Data.DataTable();        da.Fill(DT);
        如果(dt.Rows.Count大于0)
        {
            应用[产品] = DT;
        }

现在我要选择要搜索存储在应用程序从我的产品表中的关键字相关的一些数据在我的 ProductGridView

毕竟这是提高性能正确的方法?

感谢你,对不起我的英语不好;)


解决方案

  DataTable的表=(数据表)应用[产品];

之后,你可以做任何你想要与你的table.I会建议你使用会话,而不是应用

 会话[的productTable] = DT;


  

应用程序状态是一个数据存储库,提供给所有
  类在ASP.NET应用程序。应用程序状态存储在
  存储器服务器上,并且比存储和检索速度更快
  在数据库中的信息。与会话状态,这是特定于
  单个用户会话,应用程序状态适用于所有用户和
  会话。因此,应用程序的状态是存储一个有用的地方
  少量经常使用的数据的,不从一个用户改变
  另一回事。


您可以阅读文章 。请阅读最后一句,然后选择要使用的。在我的实践,我总是使用会话。

编辑:

关于评论你的问题:

首先,你不应该采取十大的搜索功能。在打开的网页中或其他地方,你应该缓存的所有元素。你会在缓存数据表此查询。如果您在前10只搜索,结果几乎每次都错了。

  SELECT
   AllImages.ImgSrc,Product.FName,Product.EName,Product.PID

   AllImages
内部联接
   产品AllImages.PID = Product.PID

有人使用搜索功能后,你会做检查是这样的:

 如果(应用[产品]!= NULL)
{
    数据表表=(数据表)应用[产品];
}
其他
{
    //从数据库中获取
}

之后,你可以使用LINQ只选择搜索词的具体数据或 DataTable.Select

  dt.Select(为ename LIKE'%+ SearchTextBox.Text +%');

之后,你是给DataTable中以网格为数据源或其它控制其使用。

注意:

在内存中的查询缓存是对数据和LT贸易; - >性能。 如果你只有具有性能困难的问题,请使用此,这是你最后的希望

我给你一个例子:

如果用户进入产品页面,我们在内存中缓存100的产品,我们会从会话/应用程序只有100个项目,但如果其他用户来创造5个新产品,他们将不会保存在内存中。你需要为他们的内存中执行新的抓取和保存。解决方案这是清除内存高速缓存在特定时间段,对于例如10分钟。 同时缓存中的数据必须在某个时刻被清除,因为你完全可以将服务器的内存!

这将保证新的数据将被放置在内存中,但你会在这十备忘录之后同样的问题。 因为是我告诉你的,这种类型的缓存是右之间的数据 - >性能交易。

im developing a search module for my website. in order to make it work faster, i want to store my Product table in Application. like this:

        DataLayer dl = new DataLayer();
        string CS = dl.dataLayerConnectionString;
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(CS);

        string FetchSqlSyntax = "SELECT AllImages.ImgSrc, Product.FName, Product.EName, Product.PID FROM AllImages INNER JOIN Product ON AllImages.PID = Product.PID";

        System.Data.SqlClient.SqlCommand FetchCmd = new System.Data.SqlClient.SqlCommand(FetchSqlSyntax , con);

        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(FetchCmd);

        System.Data.DataTable dt = new System.Data.DataTable();

        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Application["Product"] = dt;            
        }

now i want to select some data related to searched keywords from my Product table that stored in Application and display it in my ProductGridView.

After all is this a right method for increasing performance?

thank you and sorry for my bad english ;)

解决方案

DataTable table = (DataTable)Application["Product"];

After that you can do whatever you want with your table.I will advice you to use Session, not Application.

Session["ProductTable"] = dt;

Application state is a data repository that is available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.

You can read this article. Read last sentence and choose what to use. In my practice I always use session.

EDIT:

About your question in comments:

First you should not take Top 10 for search functionality. On open of the page or another place you should cache all elements. You will cache this query in DataTable. If you search only in Top 10 the result be almost every time wrong.

SELECT 
   AllImages.ImgSrc, Product.FName, Product.EName, Product.PID 
FROM 
   AllImages 
INNER JOIN 
   Product ON AllImages.PID = Product.PID

After someone use the search functionality you will make check like this:

if(Application["Product"] != null)
{
    DataTable table = (DataTable)Application["Product"];
}
else
{
    //fetch from DataBase
}

After that you can use LINQ to select only the specific data by search words or DataTable.Select method

dt.Select("ename Like '%" + SearchTextBox.Text + "%'");

After that you are giving the DataTable to your grid as DataSource or other control which use it.

BE AWARE:

Caching of queries in memory is Trade of right data <-> performance. Use this if you have only hard problems with performance and this is your last hope.

I'm giving you an example:

If user goes to Product page and we cache 100 products in the memory, we will take only 100 items from Session/Application, but if another user comes and create 5 new products they will be not saved in the memory. You need to perform new fetching and saving in the memory for them. Solution for this is to clear memory cache on specific time period, for an example 10 minutes. Also the cache data must be cleared on some point because you can full the memory of the server !

This will guarantee that the new data will be put in the memory, but you will have same problem in this 10 minutes. Because of that I told you that this type of caching is trade of between right data-> performance.

这篇关于存储在应用与选择查询如何读取一个DataTable的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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