Loading Data Sets from a Database

You may occasionally load data into a data set from an XML file that has been cached on the client machine, and you may even (more rarely) have occasion to programmatically create a data set as shown in the previous section. However, the vast majority of the time you will be using data sets in conjunction with some form of relational data store. When that is the case, you will be loading the data into a data set using one of the managed providers introduced earlier, or you will be using typed data sets, where the schema is built into the type instead of needing to be determined dynamically.

I am going to focus on the use of the SQL Server managed provider, as it is probably the most common database used in .NET applications. If you need to use the OLE DB, Oracle, or ODBC providers, the coding patterns are virtually identical, thanks to the fact that they are all based on the same interfaces. Keep in mind that the SQL Server managed provider can be used with either a full-up instance of SQL Server or with a SQL Server 2005 Express instance, which is just a scaled-down free version of the SQL Server engine.

I recommend that you use SQL Express for creating client-side or small application databases, rather than Microsoft Access databases. The SQL Express engine is much more robust for client-server applications, and you can use many features of the SQL Server engine that aren't available in Access, such as stored procedures and triggers. You can easily establish a connection to a SQL Express database just by specifying a file path to the MDF file that contains the database.

You have already seen one case of loading a data set from a SQL Server table earlier in the section Relational Data Access. You can also use a SQL Server 2005 Express database connection, as shown in Listing D.3.

0 0

Post a comment