none
Is it possible to programmatically obtain the connection string Provider property given the invariant provider name? RRS feed

  • Question

  • For example, given "System.Data.OleDB", I want to be able to obtain the value "SQLOLEDB".

    I want this functionality because I would like to programmatically create a connection string.

    I know you can do:

    Code Snippet
    DbProviderFactories.GetFactory("System.Data.OleDB").CreateConnectionStringBuilder();

     

     


    But the ConnectionStringBuilder object that's returned still doesn't "know" it's connection string provider property, even though the derived class has a provider property.
    Sunday, October 12, 2008 2:28 PM

Answers

  • If you want the list of providers, this piece of code is similar to what the designers do to get the drop-down of available OLE DB providers on the machine.

     

    Code Snippet

    foreach (string provider in TypeDescriptor.GetProperties(typeof(OleDbConnectionStringBuilder))["Provider"].Converter.GetStandardValues())

        Console.WriteLine(provider);

     

     

     

    Friday, October 17, 2008 9:51 PM
    Moderator

All replies

  • On the ConnectionStringBuilder you can just use the Item property to set the provider.

    With OleDb it will then actually attempt to load that provider when the keys are enumerated.

     

    Code Snippet

    DbConnectionStringBuilder builder = DbProviderFactories.GetFactory("System.Data.OleDb").CreateConnectionStringBuilder();

    Console.WriteLine("before");

    foreach (string key in builder.Keys)

        Console.WriteLine("{0}={1}", key, builder[key]);

     

    Console.WriteLine("after");

    builder["Provider"] = "SQLOLEDB";

    foreach (string key in builder.Keys)

        Console.WriteLine("{0}={1}", key, builder[key]);

     

    before
            File Name=
            Provider=
            Data Source=
            Persist Security Info=False
            OLE DB Services=-13
    after
            File Name=
            Provider=SQLOLEDB
            Data Source=
            Persist Security Info=False
            OLE DB Services=-13
            Integrated Security=
            Password=
            User ID=
            Initial Catalog=
            Locale Identifier=1033
            Extended Properties=
            Connect Timeout=0
            General Timeout=0
            Current Language=
            Network Address=
            Network Library=
            Use Procedure for Prepare=1
            Auto Translate=True
            Packet Size=4096
            Application Name=
            Workstation ID=MARKASH4
            Initial File Name=
            Use Encryption for Data=False
            Replication server name connect option=
            Tag with column collation when possible=False

    Friday, October 17, 2008 9:35 PM
    Moderator
  • If you want the list of providers, this piece of code is similar to what the designers do to get the drop-down of available OLE DB providers on the machine.

     

    Code Snippet

    foreach (string provider in TypeDescriptor.GetProperties(typeof(OleDbConnectionStringBuilder))["Provider"].Converter.GetStandardValues())

        Console.WriteLine(provider);

     

     

     

    Friday, October 17, 2008 9:51 PM
    Moderator