问题
In recent attempts to understand the ASP.NET GridView I have seen mst examples make use of a datasource declared directly in the ASPX markup. For example ...
<asp:SqlDataSource ID="productDataSource" Runat="server"
SelectCommand="SELECT [ProductName], [UnitPrice],
[UnitsInStock], [QuantityPerUnit] FROM [Products]"
ConnectionString=
"<%$ ConnectionStrings:NWConnectionString %>">
</asp:SqlDataSource>
I have seen a lot this sort of declarative datasource as opposed to the way I was taught (and personally prefer) of using ADO.NET to access data and then set the controls datasource in the code-behind.
Is there an advantage to using this new type of datasource declaration in the ASPX? What are the pros and cons?
回答1:
One disadvantage of the declarative way of doing the data binding is that the paging is not efficient. The gridview will pull all records from the database, and then it will only show you your page. If you have 1,000,000 records, that is not going to be fast. Doing the binding yourself, you can only pull the records you need.
For simple and small tables, the declarative method requires less development time, and the paging and sorting are built in.
For almost all real-world gridviews I have worked on, the complication of the queries to build the data set has made the declarative method not feasible.
回答2:
Declarative databinding is useful in straight-forward cases, while learning and in demos of course. As soon as you start experining with it go ahead and switch to a code-driven design and save yourself the pain.
来源:https://stackoverflow.com/questions/5319991/declarative-databinding-vs-ado-net-in-code-behind