DbProviderFactories There are several static methods for this class
Methods for SQL Server provider factory objects
DbProviderFactory fact=dbproviderfactories.getfactory ("System.Data.Client");
The GetFactory method receives a string that represents the name of the provider. The name is in the Machine.config file, which enumerates all registered providers and returns the assembly and class name information that matches the name. The factory class is not instantiated directly (that is, the so-called Singleton pattern). Once the factory object is obtained, the following methods can be called
CreateCommand returns a Command object that represents a provider-specific
Createcommandbuilder returns the provider-specific Command builder object
Crateconnection returning provider-specific connection objects
Createdataadapter returning provider-specific data adapter objects
CreateParameter returns a provider-specific Parameter object
To create a provider factory, you must provide a connection string and a provider name. This example shows how to retrieve a connection string from an application configuration file by passing the provider name in a fixed format of "System.Data.ProviderName". the code iterates through the connectionstringsettingscollection. The code returns ProviderNameon success, otherwise NULL is returned. If the provider has more than one item, the first item found is returned.
Retrieving a connection string by provider name
Retrieve a connection string by specifying the providername.//assumes one connection string per provider in the Config File.static string Getconnectionstringbyprovider (String providerName) { //Return null on failure. String returnvalue = null; Get the collection of connection strings. Connectionstringsettingscollection settings = configurationmanager.connectionstrings; Walk through the collection and return the first //connection string matching the providerName. if (settings! = null) { foreach (Connectionstringsettings cs in Settings) { if (cs. ProviderName = = ProviderName) returnvalue = cs. ConnectionString; break; } } return returnvalue;}
creating DbProviderFactory and DbConnection
The example shows how to create dbproviderfactory and DbConnection by passing the provider name and connection string in "System.Data.ProviderName" format object. the DbConnection object is returned on success, and nullis returned on error (Nothing in Visual Basic).
The code gets dbproviderfactoryby calling getfactory . Thecreateconnection method then creates the DbConnection object and sets the ConnectionString property to the connection string. ,
Given a provider name and connection string,//Create the DbProviderFactory and dbconnection.//Returns a DbConnection On success; Null on failure.static DbConnection createdbconnection ( string providerName, String connectionString) { // Assume failure. DbConnection connection = null; Create the DbProviderFactory and DbConnection. if (connectionString! = null) { try { DbProviderFactory factory = Dbproviderfactories.getfactory (providerName); Connection = Factory. CreateConnection (); Connection. ConnectionString = ConnectionString; } catch (Exception ex) { //Set the connection to NULL if it is created. if (connection! = null) { connection = null; } Console.WriteLine (ex. Message); } } Return the connection. return connection;}
So if we want to change the database simply change the corresponding connection string in the configuration file and change the createdbconnection providername parameter.