dbproviderfactory (provider factory Model)

Source: Internet
Author: User dbproviderfactory (provider factory Model)

DbProviderFactories has several static methods.

SQL Server Provider factory object Method

DbProviderFactory fact = DbProviderFactories. GetFactory ("System. Data. Client ");

The GetFactory method receives a string that represents the provider name. This name is located in the machine. config file. It will enumerate all registered providers and return the Assembly and class name information that matches the name. Factory classes are not directly instantiated (the so-called Singleton mode ). Once a factory object is obtained, you can call the following method:

CreateCommand returns a command object specific to the provider.

CreateCommandBuilder returns the specified command generator object of the provider.

CrateConnection returns the specified connection object of the provider.

CreateDataAdapter returns the provider-specific data adapter object

CreateParameter returns the specified parameter object of the provider.

To create a provider factory, you must provide the connection string and provider name. This example shows how to retrieve the connection string from the application configuration file by passing the provider name in a fixed format of "System. Data. ProviderName. The Code cyclically accesses ConnectionStringSettingsCollection. If the request succeeds, ProviderName is returned. Otherwise, null is returned. If there are multiple providers, the first item found is returned.

Retrieve the 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;}

Create DbProviderFactory and DbConnection

The example shows how to create a DbProviderFactory and DbConnection object by passing the provider name and connection string in the format of "System. Data. ProviderName. Return the DbConnection object upon success; return null when an error occurs (Nothing in Visual Basic ).

The Code calls GetFactory to obtain DbProviderFactory. Then, create the DbConnection object in the CreateConnection method and set the ConnectionString attribute 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 was created.            if (connection != null)            {                connection = null;            }            Console.WriteLine(ex.Message);        }    }    // Return the connection.    return connection;}

In this way, to change the database, you only need to change the corresponding connection string in the configuration file, and change the providerName parameter of CreateDbConnection.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.