DataSource Overview










SQLDataSource Vs ObjectDataSource
By Barbie Hocking  ©2010
         
The ASP.Net SQLDataSource and ObjectDataSource are used to databind data bound controls such as the GridView.  Their ease of use differs along with their flexibility and architectural strengths.

The SQLDataSource has a simple point and click interface which requires a minimal amount of code.  It allows you to easily create Select, Insert, Update, and Delete commands that can be executed against a Sql Server data source.  Paging is easily incorporated into the design. 

The ease of use results in the user interface, business logic, and data layer co-existing within the form or control.  Although this is not an optimum design, it is effective for single-tier applications.  However, multi-tiered applications need not discount the SQLDataSource. It can still be effectively used for functionality such as admin table maintenance.
The ObjectDataSource uses custom objects. These can be class objects or DAL (Data Access Layer) objects.  The use of class objects allows the application to be architected into a Business Layer/Database Layer structure, which is a best practices architecture.  Paging can be incorporated into ObjectDataSources, but require additional coding.

Keep in mind that you don’t have to use a SQLDataSource or ObjectDataSource.  You can also programmatically define a datasource and databind it to a data control.

SQLDataSource ObjectDataSource
Provides fast point & click setup
Binds Presentation Layer to object
Supports SQL Server data provider
Supports multiple data providers (i.e Oracle)
Supports multiple data types (i.e. XML)
Supports complex business logic
Supports shared business logic
Provides paging support*
* SqlDataSource: Automatically handles paging. Default settings load the entire resultset in cache so should not be used for large results.
ObjectDataSource: Paging requires additional configuration & SQL coding. Resultset contains current page data only.


C# Sample Code
The sample code contains a Visual Studion web application with four DataBinding samples.
  1. Programatic DataBinding
  2. SQLDataSource DataBinding
  3. ObjectDataSource DataBinding with a Data Access Layer (DAL). Refer to Create a DAL for an ODS for creating a DataAccessLayer instructions.
  4. ObjectDataSource DataBinding with a class object
Very little coding is needed for this basic sample.  Download the sample code to view the code-behind requirements. The web form is detailed below:
Programatic DataBinding Sample: companyName like 'a%'
<asp:GridView ID="gvDataBindSample" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" SkinID="default" >
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
</Columns>
</asp:GridView>

SQLDataSource Sample: companyName like 'b%'
<asp:GridView ID="gvSqlDataSourceSample" runat="server" AutoGenerateColumns="False"
DataKeyNames="CustomerID" DataSourceID="sdsCustomers" SkinID="default" >
<Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsCustomers" runat="server" ConnectionString="<%$ ConnectionStrings:myConnection %>"
SelectCommand="SELECT * FROM Customers where companyName like 'b%'"></asp:SqlDataSource>

ObjectDataSource DAL Sample: companyName like 'c%'
<asp:GridView ID="gvObjectDataSourceSample1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="odsCustomers" SkinID="default" >
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsCustomers" TypeName="CustomerSampleTableAdapters.CustomersTableAdapter"
SelectMethod="GetCustomersLikeC" runat="server"></asp:ObjectDataSource>

ObjectDataSource Class Object Sample: companyName like 'd%'
<asp:GridView ID="gvObjectDataSourceSample2" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="odsCustomers2" SkinID="default" >
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsCustomers2" TypeName="CustomerDataAccess"
SelectMethod="GetCustomersLikeD" runat="server"></asp:ObjectDataSource>
        
 Contact Us     Links      ©2010 GeekPhilosopher.com - All rights reserved
Powered by www.ezjooz.com