Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic connection string in DAAB

Tags:

c#

.net

daab

I am having a situation where i need multiple connection strings to process a request, but i am not able to implement this using the Microsoft enterprise library.

somebody please tell me how can i set the connection string dynamically.

found similar questions on internet, but none solving my problem, hence posted.

like image 798
Bravo Avatar asked Feb 17 '26 13:02

Bravo


1 Answers

AFAIK, Its not possible to set the connection string dynamically, however you can write a Custom database factory to create a database with dynamic connection string

    public static class CustomDatabaseFactory
    {
        static readonly DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

        public static Database CreateDatabase(string connectionString)
        {
            return new GenericDatabase(connectionString, dbProviderFactory);
        }
    }

and instead of calling this

Database db = DatabaseFactory.CreateDatabase();

call this method

Database db = CustomDatabaseFactory.CreateDatabase(connstring);

worked well for me!

like image 103
SKR Avatar answered Feb 19 '26 02:02

SKR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!