ASP.NET Basics (Part 9): Different Strokes
(Page 1 of 5 )

You should now know how to get data out of a database with ADO.NET, but how about putting it in? In this article, continue your journey into the world of database manipulation with ASP.NET by learning different techniques to view, add and edit records in a database.
Moving On Up
Last time out, I gave you a quick introduction to ADO.NET and how it differs from its predecessor, together with an example of how to get a connection up and running between an ASP.NET page and a SQL Server 2000 RDBMS. I also introduced you to some of the basic objects used to retrieve and iterate over a record set from the database, including the ASP.NET DataGrid server control which lets you quickly display a result set table.
Of course, I'm not done yet; all I've shown you so far is how to get data out of the database. But what about putting it in, or changing whatever's already inside? Well, that's where the SqlDataAdapter and DataSet objects come in; they work closely together to give developers full-fledged database access, including the ability to write new records or edit/delete existing records. Over the course of this article, I'll show you how to use these two objects to carry out the entire spectrum of SQL operations, including INSERTs, UPDATEs and DELETEs. So keep reading.
{mospagebreak title=Setting Up}
Setting Up
In the last segment of this tutorial, I used a SqlDataReader object to retrieve records and display them with a "while" loop. Sadly, that's about all you *can* do with this object - if you remember what I said last time, the SqlDataReader object returns a read-only set of records which can only be accessed in the forward direction. Furthermore, as mentioned earlier, the SqlDataReader object is meant for a connected system, wherein a database connection is maintained between the server and the client for the entire duration of the session.
Needless to say, this can affect system resources significantly. Which is why ADO.NET also supports "disconnected data access", via two other objects: the SqlDataAdapter object and the DataSet object.
First, the SqlDataAdapter object acts a bridge between the data stored in the database and a DataSet object, which is used to display to the data to the end user. This DataSet object is used to the hold the data ferried by the SqlDataAdapter from the database, and it works like a mini-database by caching the data locally on the client and allowing the user to make changes to it. Once the changes are complete, the SqlDataAdapter object updates the live database with your changes.
Since this DataSet is cached locally on the client, this approach allows for speedy access and manipulation of data, without having to keep the connection to the server open at all times. Further, the DataSet object even supports XML and can thus be used to create XML representations of the query result set and send it over HTTP to any location on the network (internal or external). In this serialized XML form, the result set becomes an ordinary string of data hat can easily be transferred across a network without the need for proprietary protocols.
What does this translate to in terms of code? Let's see.
Next: The XML Files >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire