Page 1 of 1

Chaning DataBase connection at runtime

Posted: Tue 29 May 2012 20:40
by drysg
I can easily use LinqConnect at Design time to set a fixed connection string. But some of our users will need to change the port and login information at runtime.

This is no problem with ADO connections (see below) but how does one do this with LinqConnect where the only handle on has is the context that one created from the model

DBmodel context = new namespace.DBmodel();


ADO version:

Code: Select all

Devart.Common.DbConnectionStringBuilder csb = new Devart.Common.DbConnectionStringBuilder();
            csb["Host"] = "localhost";
            csb["Port"] = 5432;
            csb["User Id"] = "postgres";
            csb["Password"] = "*******";
            csb["Database"] = "GPL";
            csb["Max Pool Size"] = 150;
            csb["Connection Timeout"] = 30;
            PgSqlConnection conn = new PgSqlConnection(csb.ConnectionString);

Re: Chaning DataBase connection at runtime

Posted: Wed 30 May 2012 13:30
by MariiaI
The DataContext class has several overloads of the constructor, in particular, one taking the connection string.
For example:

Code: Select all

string connectionString = "User Id=user;Password=password;Host=host;Port=5432;Database=databasename;Persist Security Info=True";
PostgresDataContext dc = new PostgresDataContext(connectionString);