The pass Wednesday June 15, I give an ASP.NET 2.0 talk for the .NET Mexico City User Group and the presentation was about the data access objects we can use in this new version of ASP.NET.
Do you know what DataSource Controls in ASP.NET 2.0 are?
DataSource Controls are and intermediate object between your database calls and your data-binding controls, you know, in ASP.NET 1.1 we need to write ADO.NET code using connections/command objects and DataSet or DataReader. Know in ASP.NET 2.0 we only need a knew set controls called xxxDataSource (xxx = Sql, Object, Access, Xml, SiteMap) witch control the interactions whit a physical data source, the nice thing is that you don’t write any line of code, only with declarative tags you can configure this object and used to bind you data with you controls.
<asp:SqlDataSource ID=”Employees” RunAt=”server” ConnectionString=”server=localhost;database=mydatabase;integrated security=true” SelectCommand=”select FirstName, LastName from Employees” />
<asp:DataGrid DataSourceID=” Employees” RunAt=”server” />
As you can see in this basic example we use the SqlDataSource object to declare who is our data source (connectionstring) and what data I need (SelectCommand). Then in the DataGrid control we only specify (DataSourceID) what DataSource is going to be our intermediate database object.
The result is cero code to bind our datagrid to our employees table.
This control is very rich because we can specify insert, update, delete command and also parameters that maybe come from QueryString, Forms, Controls, etc. If you need to connect with other data sources like Oracle, with the ProviderName property you can specify a Manage Provider like “System.Data.OracleClient” and boooommmm, that’s all. By the way, the connection string it’s different.
What if you want to use code?
Putting the connectionsstring and SQL Sintax in our aspx code it’s not a great practice, so, you maybe asking, and where the code is?, what if I want to use ADO.NET code. Well one answer is the ObjectDataSource Control.
ObjectDataSource control has the same behavior describe in SqlDataSource but the data source we can use is a middle-tier component. So in this component you maybe have the Insert, Update, Delete, Select methods and you only use the SelectMethod, UpdateMethod, DeleteMethos, UpdateMethod properties to declaratively associate you class members with this datasource control.
Another great big usefully features this controls have, you can enable caching. Well I have to go, but latter im going to write more about this great data access technology.