Here we take a closer look at "generic" methods that can display data from ANY field from ANY data source.
This is extremely useful in cases where the data source is initially unknown, and allows for setting different field headers and retrieving field values dynamically, without having to know and explicitly code each field name.
The samples presented below makes use of schema information to dynamically retrieve the names of the available tables and their associated columns in a given database.
The SQL Server™ .NET Data Provider exposes schema information through informational views, while the OLE DB .NET Data Provider exposes schema information using the GetOleDbSchemaTable method of the OleDbConnection object.
The following examples demonstrate using these methods to dynamically retrieve the schema and subsequently the data for both SQL Server™ and OLEDB data sources. Both of these methods return a special type of DataTable object containing the database schema, which we use here to populate an <asp:DropDownList> control, enabling the user to choose which table, and which columns in each table, to obtain the data to display.
Now, let’s explore the significant parts of the logic for each provider.
Basically, to obtain the list of tables for a given SQL Server™ database:
string query = "SELECT * FROM Information_Schema.Tables " +
"WHERE Table_Type='Base Table';
And to obtain the columns for a given table:
string query = "SELECT * FROM Information_Schema.Columns WHERE Table_Name='" +
tableName + "’";
where tableName is the table to extract the columns collection from, and which, in this excercise, is obtained from user input.
The following sample demontrates using the above concepts to first extract the table information from an SQL Server™ database, then extract the column information from the selected table.
Basically, to obtain the list of tables for a given OLEDB database:
object [ ] restricts = new object [ ] {null, null, null, "Table"};
DataTable dbTables = myConn.GetOleDbSchemaTable (
OleDbSchemaGuid.Tables, restricts );
And to obtain the columns for a given table:
object [ ] restricts = new object [ ] {null, null, "tableName", null};
DataTable dbFields = myConn.GetOleDbSchemaTable (
OleDbSchemaGuid.Columns, restricts );
where tableName is the table to extract the columns collection from, and which, likewise in this excercise, is obtained from user input.
The following sample demontrates using the above concepts to first extract the table information from an MS Access database, then extract the column information from the selected table.
To simply display the selected data without using any control, you can adopt the generic display method described in Retrieving Data Using a DataSet, which can be used to display any DataSet into a grid-like table view.
Using the same concepts, and with just a slight change to the scripts, we could use a variation of the display method described in Retrieving Data Using a DataSet, only here, we render the results of the database queries into a "list" view.
Well, that’s about it for this session.
Hopefully at least, you’ve gained some helpful hints to fill your .NET bag of tricks.
Modesty aside, though, the samplers presented in this section involve simple, power features that can surely find use in varied data-based apps. The pages can serve as reusable templates that can be adopted for most uses, simply by changing the data source.
Of course, they can further be enhanced, but I wouldn’t want to spoil the fun, so I’ll leave that up to you.
In succeeding articles, we shall cover the key concepts and methods to enable users to search for, add, edit, and delete information first at the database command level, then at the DataSet level.
Populating a List Web Server Control from a Database