BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
Yes, ASP.NET provides DataSource controls, but wizards only get you so far. Share the secret of how Wise Owl create all our ASP.NET websites, using a single, simple data class.
- Data Classes in ASP.NET
- Our Example for this Data Classes Tutorial
- Storing and Retrieving Connection Strings
- ADO.NET: DataSets, DataTables and Data Adapters (this blog)
- Consuming our Data Classes
- A Class to Run a Stored Procedure and Return its Rows
- Consuming our Data Class on the Web Page
Posted by Andy Brown on 07 August 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
ADO.NET: DataSets, DataTables and Data Adapters
ADO.NET is the technology used within .NET to connect to databases (if you're interested, ADO originally stood for ActiveX Data Objects - but you're probably not).
You can actually get away without understanding any of this page - just copy and paste the sample code towards the end of this blog - but I think a little theory is sometimes useful!
Your code is going to be making a lot of use of DataTables, DataRows and DataSets. These all belong in the System.Data namespace. It's a bit tedious to have to type this in every time, so it makes sense to reference this within our code files. Here's how to do this in C#:
// allow use of DataTables, DataRows, etc.
// allow use of SQL Server objects
Here's the VB equivalent:
'allow use of DataTables, DataRows, etc.
'allow use of SQL Server objects
I've also referenced here the System.Data.SqlClient namespace, so that we can reference objects like SqlCommand without having to fully qualify them.
DataSets, DataTables and DataAdapters
We start out with a table stored in SQL Server (or any other database, for that matter):
We want to get rows from this table into memory in our website.
To do this, we'll create a DataSet in memory within VB or C#, consisting of one or more DataTables:
A DataSet can contain lots of tables - although ours will only contain one.
To get the rows of data from the SQL Server table we'll use something called a DataAdapter, which is basically a set of SQL statements designed to select, insert, update and delete data. Here's how this works:
We'll use a DataAdapter to fill a table in a DataSet, thus transferring data from the underlying database's tblMovies table into a table of records in memory.
And that's about all that you need to know about ADO.NET to create data classes!