Monday, March 30, 2009

ASP.Net and Databases – Tip for making databinding faster

Do you set the DataSourceMode property on your DataSources?

image The DataSourceMode property on DataSources (such as SqlDataSource) allows you to specify whether the reading of the data will be performed using a DataReader or a DataSet.

Why does this matter? A DataReader is a forward only reader and hence is faster and requires less memory. On the other hand DataSets are allow you to read, update, insert, delete data and hence require more memory and are slower than DataReaders. Knowing this is important, because if you are loading data into a ComboBox, why use a DataSet, when a DataReader would do?

Remember: The default value for DataSourceMode is DataSet.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks2008ConnectionString %>" 
            DataSourceMode="DataReader" 
            SelectCommand="SELECT ProductCategoryID, Name FROM Production.ProductCategory"></asp:SqlDataSource>

No comments: