简体   繁体   English

在asp.net(vb.net/c#)中从ms sql数据库填充数据的更好方法

[英]Better way for populating data from ms sql database in asp.net (vb.net/c#)

What is better way for populating data from database (table approx. 500 rows with min.5 cols): - using GridView (and I'll get huge viewstate value in header)?, or - using Repeater with Literal controls with enableviewstate="false" (but it must be populated after every postback , using tr with onclick attribute and hidden field, JavaScript)? 有什么更好的方法来填充数据库中的数据(表,每行至少500行,最少5行):-使用GridView (并且我将在标头中获得巨大的viewstate值)?或-将Repeater与具有Literal控件的enableviewstate="false"一起使用enableviewstate="false" (但必须在每次postback后使用带有onclick属性和隐藏字段的tr使用JavaScript进行填充)?

Is better way with large viewstate or reading mdf after every postback ? 是较大的viewstate还是在每次postback后读取mdf的更好方法?

Thx. 谢谢。

It depends on where your concern lies. 这取决于您的担忧所在。

If ease of handling events is your main concern, the gridview with viewstate enabled is your guy. 如果您最关心的是事件的易处理性,那么启用viewstate的gridview是您的最佳选择。 It has great hook-ins for all kinds of actions the user may take. 它具有用户可以执行的各种动作的强大挂钩。 It does, however, come with the viewstate bandwidth cost you mentioned. 但是,它确实附带了您提到的viewstate带宽成本。 But if you're audience uses relatively good connections and you need to get up and running fast, this can be a good alternative. 但是,如果您的受众使用相对良好的联系并且需要快速启动并运行,那么这可能是一个不错的选择。

If bandwidth is your main concern, the repeater/literal approach will definitely save you some, but you'll have to write all your event logic yourself. 如果带宽是您主要关心的问题,那么中继器/字面方法肯定会为您节省一些时间,但是您必须自己编写所有事件逻辑。 The running of the same query for each postback is really redundant, but would in reality probably not take up that much perf, because of the db's caching mechanisms. 对每个回发运行相同的查询确实是多余的,但实际上由于数据库的缓存机制,它可能不会占用那么多的性能。 However, you would still be sending back the complete table after each postback. 但是,每次回发后,您仍然会发送完整的表。 The best bandwidth saving scenario, in my opinion, would be sending the complete table only once on load, and after that handle all table events with web service calls (you can use ASP.NET's Web-API framework for that). 我认为,最好的带宽节省方案是在加载时仅发送一次完整的表,然后再通过Web服务调用处理所有表事件(您可以使用ASP.NET的Web-API框架)。 That way, only any actual changes would be sent over the wire. 这样,只有任何实际的更改将通过网络发送。

You can also disable the viewstate on the GridView and that will make it faster. 您还可以在GridView上禁用viewstate,这将使其更快。 The Repeater is still faster than the GridView, but the difference shouldn't be big if you code it right. Repeater仍然比GridView快,但是如果编码正确,差异不会太大。 Also you can consider paging the GridView (ie displaying it in pages, let's say 10 rows at a time). 您也可以考虑分页GridView(即在页面中显示它,例如一次显示10行)。

Reading from the database is usually faster and more efficient than sending large ViewState back and forth, but it's not only the speed that determines which way to go. 从数据库中读取数据通常比来回发送大型ViewState更快,更高效,但不仅仅是速度决定了走哪条路。 There is security and other matters to consider. 需要考虑安全性和其他事项。 For example, getting the data from the database is much better for security than trusting the ViewState which can be tempered with easily. 例如,从数据库获取数据对于安全性要比信任可以轻松调整的ViewState好得多。

If the data you expect to be rendered is heavy, why don't you implement any client side implementation? 如果您希望呈现的数据繁重,为什么不实施任何客户端实现? Like jqGrid or using KnockoutJS API? 像jqGrid还是使用KnockoutJS API? Then you can build a web service as the backend data supplier. 然后,您可以将Web服务构建为后端数据提供者。

You can start with jqGrid here and KnockoutJS here 您可以从这里的 jqGrid和KnockoutJS 开始

If you implement this using client side libraries, the application will perform better since there is no need of a heavy viewstate to persist the values and the server will not need to render the markup everytime. 如果使用客户端库实现此功能,则由于不需要繁琐的视图状态来保留值,并且服务器无需每次都呈现标记,因此应用程序将性能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM