DatabaseFactory.CreateDatabase

Topics: Data Access Application Block
Sep 10, 2007 at 11:10 PM
When we call DatabaseFactory.CreateDatabase with a parameter, are we simply using that paramater to match with the name of the connection strings defined in Enterprise Library Configuration?

Is the following information from the Enterprise Library Help correct? I believe that statement only creates a database object myDb not a database with the name "Sales."

The following example shows how to create a database with the name "Sales."
Dim myDb As Database = DatabaseFactory.CreateDatabase("Sales")


Sep 11, 2007 at 11:41 AM
Hi,

The DatabaseFactory will certainly not create a new database in a server, if that was your concern. A more precise decription would be: "retrieves the connection string with name 'Sales' from configuration and creates an instance of the concrete subclass of the Database class that has been mapped to the connection string's ADO.NET 2.0 provider, configured with the connection string's connection information".

Fernando
Sep 11, 2007 at 6:03 PM
Hi Fernando,

Thanks. I just feel that the parameter of DatabaseFactory.CreateDatabase() should be described as the name of a connection string instead of database instance or other similar terms in many references.


Peter
Sep 13, 2007 at 11:35 AM
Edited Sep 13, 2007 at 11:36 AM
Hi,

I want use dynamic connection string at runtime in the code. I want to do it using enterprise library 3.0.Please help me in this. this is very urgent.

solution please forword it to my hotmail id: sreeramg@hotmail.com also.



Regards,

Sreeram
Sep 13, 2007 at 7:03 PM
Hi,

You can always create the Database instance you want and pass the connection string in the constructor, like: Database myDb = new SqlDatabase(myConnectionString);.

Regards,
Fernando
Sep 13, 2007 at 10:18 PM
Hi Fernando,

Instead of passing the connection string, is it possible to assign a new connection string to System.Configuration.ConfigurationManager.ConnectionStrings(XXX).ConnectString?
Sep 17, 2007 at 7:11 AM


pcsql wrote:
Hi Fernando,

Instead of passing the connection string, is it possible to assign a new connection string to System.Configuration.ConfigurationManager.ConnectionStrings(XXX).ConnectString?


Thanks for response.Can you elaberate it. Otherwise you send code sample which you have/URL related to this topic? Here again i am attaching my query.

"I have requirement dynamic connection string at runtime in the code and have to use enterprise library 3.0.This connection string will be build at run time. As i understand from enterprise library 3.0 is that, we can set connection string at web.config in design time.But i will get my connection string from definistion form(Screen).So I will store it in string and i will pass it to object.That is my intension.Please help me in this. this is very urgent."

Regards,
Sreeram





Regards,

Sreeram


Sep 17, 2007 at 7:12 AM


fsimonazzi wrote:
Hi,

You can always create the Database instance you want and pass the connection string in the constructor, like: Database myDb = new SqlDatabase(myConnectionString);.

Regards,
Fernando



Thanks for response.Can you elaberate it. Otherwise you send code sample which you have/URL related to this topic? Here again i am attaching my query.

"I have requirement dynamic connection string at runtime in the code and have to use enterprise library 3.0.This connection string will be build at run time. As i understand from enterprise library 3.0 is that, we can set connection string at web.config in design time.But i will get my connection string from definistion form(Screen).So I will store it in string and i will pass it to object.That is my intension.Please help me in this. this is very urgent."

Regards,
Sreeram





Regards,

Sreeram


Sep 17, 2007 at 12:18 PM
Hi,


sreeramstuff wrote:


fsimonazzi wrote:
Hi,

You can always create the Database instance you want and pass the connection string in the constructor, like: Database myDb = new SqlDatabase(myConnectionString);.

Regards,
Fernando



Thanks for response.Can you elaberate it. Otherwise you send code sample which you have/URL related to this topic? Here again i am attaching my query.



Look at section Creating a Database Without Configuration on this topic ms-help://ms.EntLib.2007May/EnterpriseLibrary/html/04-070-Details_of_Creating_a_Database_Object.htm. There isn't much more to this that the code I posted though.

Of course with this approach you need to know the kind of Database object you will create, which may not be possible in your scenario.

Fernando
Aug 17, 2008 at 10:39 PM
The above link does not work. Can you please send me some more  online resources or snippet of the code where you can construct the connection string dynamically in Ent Lin 3.0. I do not want to put 300 database instance name in web.config.
ms-help://ms.EntLib.2007May/EnterpriseLibrary/html/04-070-Details_of_Creating_a_Database_Object.htm
Aug 19, 2008 at 2:15 PM
Hi,

Here's the topic @ msdn http://msdn.microsoft.com/en-us/library/cc309175.aspx.

I don't understand what you mean by having 300 database instance names in web.config; do you mean connection strings?

Regards,
Fernando
Sep 15, 2008 at 5:20 PM
All,

This thread helped me solve my problem which was similar to the original poster.

I created a connection string in my WebConfig file which serves as a template.
In my code I use 

Database GetMyDatabase()
    {
        Database configdb = DatabaseFactory.CreateDatabase("MY_CONNECTION_STRING");

 

            DbConnectionStringBuilder sb = configdb.DbProviderFactory.CreateConnectionStringBuilder();

 


            // Set the Connection String fields.
            sb["Data Source"] = "ORADB001";
            GenericDatabase newDb = new GenericDataBase (sb.ToString(), configdb.DbproviderFactory);
            return newDb;

}


Works great.
I still wish that the Enterprise Database object had a settable ConnectionString property.
Apr 8, 2009 at 3:36 PM
I am using Sql Server 2005 , In that we are having different databases(Ex: db1, db2,db3).
Basing on login(Web application) we need to change the database names.
For Ex :  user1 ---db1, User2 ----db2

How to achieve this by using GenericDatabase method (or) any other way.

This is Urgent Requirement. Help me plz


Regards
Phani
Apr 13, 2009 at 9:33 AM
Hi,

First of all, I want to clear the question, as I understand it, if I'm a user and I logged in to your application, after i get authenticated then I'l be able to connect to the database for me? is that correct? Well if that is correct, the next thing that I can think of is, if do you have the facility to determine which database for that use that has logged in. If you have that facility, I think it would be straight-forward, if you now know which database to connect to then you can construct the connection string then, you can do it something like this "Database db = new SqlDatabase("Data Source=localhost;Initial Catalog=databaseOfTheUser;Integrated Security=true;");". I'm just not sure how this will fit to your current code implementation. Please give it a try.


Valiant Dudan
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 25, 2010 at 5:09 AM
We are using SQL Server 2005 and Enterprise library 4.1, In that we are having different databases (Ex: db1, db2, db3). Based on login (Web application) we need to change the database names. For Ex: user1 ---db1, User2 ----db2 As per our current code implementation we are not using any wrapper class for creating database object. We are using like as follows where we require to connect database Dim db As Microsoft.Practices.EnterpriseLibrary.Data.Database = DatabaseFactory.CreateDatabase("user-db") " user-db" connection string is stored in web.config. As per your this post reference, We can use something like this "Database db = new SqlDatabase("Data Source=localhost;Initial Catalog=databaseOfTheUser;Integrated Security=true;");". But to implement this solution we have to do lot of change in our existing code because we have followed Dim db As Microsoft.Practices.EnterpriseLibrary.Data.Database = DatabaseFactory.CreateDatabase(" user-db ") this approach. We are very thankful to you if can suggest any way to implement this solution with minimal code change. You can reply on above thread or you can directly mail me. Thanks in advance
Feb 25, 2010 at 6:26 AM

The minimal code change for this scenario would be definitely to change the way how you're creating the Database object, you can't avoid that.  I suggest you create a wrapper class for this so your code will be flexible enough whenever you will be required to do something different when creating database objects. 

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com