How can i change ConnectionString at runtime in Data Access Application Block?

Hello,
I used Enterprise Library 2.0 in my application. i want to know that is it possible to change ConnectionString at runtime if we are used Data Access Application Block.
Well i have company list in my application and on that based i have to change database name in ConnectinString on selected company name.



Answer this question

How can i change ConnectionString at runtime in Data Access Application Block?

  • Nrupesh

    Hi,

    Or more properly:

    conn.close()
    conn.ConnectionString = connstr
    conn.open()

    cheers,

    Paul June A. Domag



  • madhi

    but we are not be able to pass connection string in Data Access Application Block because we have to declare connectionstring in app.config file. so when we are going to create database connection Data Access Application Block read from .config file. so in this case your answer will not work.

  • Gradinariu Cezar

    try closing the conn

    conn.close()
    conn = new conn(connstr)
    conn.open()


    thats alll.

  • BobTheBuild

    If these are all different databases within the same SQL Server instance, consider using:

    DbConnection.ChangeDatabase( string databaseName )

    Look up this link in your local MSDN installation:

    ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref4/html/M_System_Data_Common_DbConnection_ChangeDatabase_1_16219e3a.htm



  • english_d

    A better option might be to add all the connectionstrings needed to app.config, and then dynamically pass the connection name when creating a Database instance.


    Dim db As Database = DatabaseFactory.CreateDatabase("DEBUG")

    <connectionStrings>
    <add name="DEBUG" providerName="System.Data.SqlClient" connectionString="server=server01;database=DEBUG;Integrated Security=True;"/>
    <add name="PROD" providerName="System.Data.SqlClient" connectionString="server=server01;database=PROD;Integrated Security=True;"/>
    <add name="TEST" providerName="System.Data.SqlClient" connectionString="server=server01;database=TEST;Integrated Security=True;"/>
    </connectionStrings>

    If you have over 20 different connections than you might not want to use the Data Access block.

  • jcsam

    Hi,

    OK I'm a bit lost here. Could you further explain what's inside your application block Are you using typed datasets Or a certain kind of datalayer Any additional info would be great. :)

    Does your Application Block contain a Connection object If so, every connection object has a ConnectionString property and the connection string property is a string that you can modify freely.

    cheers,

    Paul June A. Domag



  • How can i change ConnectionString at runtime in Data Access Application Block?