1

May I ask if you have a sample code snippet that I can start with in working with Enterprise Library 5 application block? The requirement is for me to dynamically change connection string at runtime. The connection string will not come from config file, instead from a sql server database. The sql server database will then have a table containing the list of connection strings to different databases that will be maintained by a group of admins. Initially, this table of connection strings will contain connections to a DB2 and SQL Server Database.

I checked this link:changing conn string at runtime

bu I can't make it work. Is there a simple way in doing this in Enterprise library?

Thank you.

Community
  • 1
  • 1
Bon Perez
  • 55
  • 1
  • 5

1 Answers1

3

Connect to your first DB, pull out the list of connection strings and then connect to the other DB like so:

database mydb = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase("connection string here");

Rocklan
  • 7,888
  • 3
  • 34
  • 49
  • how about if i want to connect to an DB2 database? – Bon Perez Sep 04 '12 at 02:16
  • If it's a DB2 database then the connection string will need to be of this format "Server=myAddress:myPortNumber;Database=myDataBase;UID=myUsername;PWD=myPassword;". See http://www.connectionstrings.com/ibm-db2 for more info. – Rocklan Sep 06 '12 at 03:59
  • The full namespace is Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase so the code would look like `var db = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase("connectionStringHere");` – styfle Jun 01 '13 at 00:04