Nonstop Database, HiRDB Version 9 UAP Development Guide

[Contents][Index][Back][Next]

16.9 Provider-independent codes using DbProviderFactory

ADO.NET2.0 can now create instances of other classes, such as Command and Parameter, by using the DbProviderFactory instance. You can create an instance specific to a provider based on the provided information by creating a DbProviderFactory instance with a character string indicating the specified provider's namespace (referred to hereafter as the provider name). You can register a provider name in a configuration file for later acquisition. This enables you to create provider-independent codes and select a desired provider during execution.

Organization of this section
(1) Adding provider information
(2) Specifying provider names by using a configuration file
(3) Creating a DbProviderFactory instance
(4) Sample coding

(1) Adding provider information

You create a DbProviderFactory instance by calling the GetFactory method of the DbProviderFactories class. When you install .NET Framework version 2.0 or 3.0, you can specify the following four types of provider names with this method:

You can specify provider names in the GetFactory method if you add provider information to the DbProviderFactories element of the system.data section in the machine.config file that is installed when you install .NET Framework version 2.0 or 3.0.

For HiRDB.NET Data Provider, the invariant value to be specified is Hitachi.HiRDB.

The following shows an example that adds information about HiRDB.NET Data Provider.

(2) Specifying provider names by using a configuration file

You can register provider names in a configuration file for later acquisition when you create a DbProviderFactory instance. For details about the configuration file, see the documentation of .NET Framework.

For HiRDB.NET Data Provider, the provider name (value) to be specified is Hitachi.HiRDB. Note that a configuration file is not required if you specify a provider name directly in the argument of the GetFactory method of the DbProviderFactories class.

The following shows an example of a configuration file. The key name (provider) can be any character string.

(3) Creating a DbProviderFactory instance

(a) When a configuration file is not used

This example specifies the provider name (Hitachi.HiRDB) directly in the argument of the GetFactory of the DbProviderFactories class and then creates a DbProviderFactory instance. The following shows sample coding:

 
DbProviderFactory dataFactory =
           DbProviderFactories.GetFactory("Hitachi.HiRDB");
 
(b) When a configuration file is used

This example acquires the value of the key name (provider) from the configuration file and then creates a DbProviderFactory instance by specifying that value in the argument of the GetFactory method of the DbProviderFactories class. The following shows sample coding:

 
DbProviderFactory dataFactory =
           DbProviderFactories.GetFactory(ConfigurationManager.AppSettings["provider"]);
 

(4) Sample coding

By acquiring the provider name and connection character string that depend on the provider from the configuration file, you can use different providers without having to change the program.

The following shows sample coding for using a configuration file:

[Figure]