分配到数据表视图状态是一个好办法? [英] Assigning DataTable to ViewState is a good way?

查看:94
本文介绍了分配到数据表视图状态是一个好办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取一个DataTable,并分配给ViewState的象下面这样:因为我不想打数据库每一次

I'm getting a DataTable from a DataBase and assigning to ViewState like below: Because I don't want to hit the database for every time.

DataTable dt = GetDataTable();
ViewState["dtTable"] = dt;

GetDataTable()是一个方法,它从数据库中获取1000条记录。
这是最好的方法,或其中之一是处理最好的方法?

GetDataTable() is a method, which retrieves 1000 records from the DataBase. Is this the best way or which one is the best way to handle this?

推荐答案

首先第一件事情:一边解释,还是要看很多您的要求,环境设置...

First things first: Explanations aside, It still depends a lot on your requirements, environments settings ...

视图状态存储在呈现为℃的隐藏字段输入/> 发送到浏览器的最终的HTML标签。当用户启动一个回发(点击按钮等),将数​​据发送回服务器提交的表单数据的一部分。

The viewstate is stored in a hidden field rendered as <input /> tag in final HTML sent to browser . When the user initiates a postback ( Button Click etc..), the data is sent back to the server as part of the submitted form data.

如果你存储大量的ViewState的数据,你将不得不承担一个点球,当用户尝试下载的页面,因为所有这些数据将成为你的HTML的一部分,同时当用户试图提交表单,因为再次此数据将被发送回服务器。

If you store large amounts of data in the ViewState, you will have to incur a penalty when the user attempts to download the page because all such data will be part of your HTML and also when the user attempts to submit the form because again this data will be sent back to server.

在此外,ViewState是容易丢失。它只有preserved只要用户正在提交的形式。如果用户点击一个超链接到另一个页面,表单不会提交,因而包含的ViewState中的数据丢失。

In addition, the ViewState is easily lost. It is only preserved as long as the user is submitting the form. If the user clicks a hyperlink to another page, the form is not submitted and therefore data contained within the ViewState is lost.

有建议使用视图状态是如果数据是比较小的。

It is recommended to use ViewState is if the data is relatively small.

如果我们考虑安全选项,ViewState的数据连接以base64 codeD,它可以很容易地去codeD。这是黑客攻击网站的典型例子,所以交叉检查,以什么样的数据正是你所存储。虽然你可以通过设置解决这个问题 EnableViewStateMac 为true。

If we consider security options, ViewState data is encoded in base64 , which can be easily decoded. This is a classic example for hacking a website, so crosscheck as to what data exactly you are storing. Although you can overcome this issue by setting EnableViewStateMac to true.

对于大数据量的,Session是一个不错的选择。如果你能在任何用户与特定的数据块进行检测,设置会话变量设置为null,以对抗内存开销。你不能总是这样做,而且会议将过期,内存会被自动回收。降低会话超时也可以帮助,但将其设置为按要求需要。

For large amounts of data, Session is a good option. If you are able to detect when any user is done with a particular block of data, set the Session variable to null, to counter memory overhead. You can't always do this, but Also the Session will expire and the memory will be reclaimed automatically. Lowering the Session timeout can also help but set it as per the requirement needs.

此外,在会话中的数据实际上是$页面加载的Web服务器页上$ psent。这有助于保持页面尺寸小,它只有利用该会话ID

Also,the data in the Session is actually present on the web server between page loads. This helps in keeping the page size small, it only has to utilize the Session ID.

最后一个选择是使用Caching.Check MSDN这里的最佳实践上高速缓存。

One last option is to use Caching.Check MSDN here for the best practices on Caching.

这篇关于分配到数据表视图状态是一个好办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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