Connecting Tech Pros Worldwide Forums | Help | Site Map

Connect to Database

shapper
Guest
 
Posts: n/a
#1: Jun 21 '07
Hello,

I am using Microsoft Enterprise Library, on a class, to connect to a
database as follows:

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

However, I would like the database name to be dynamic, i.e., specified
on the web site.

Can I define a connection string in my web site web.config and then
use it to create the database?

Or can I create my own Web.Config group to hold this information?

Thanks,
Miguel


Cowboy \(Gregory A. Beamer\)
Guest
 
Posts: n/a
#2: Jun 21 '07

re: Connect to Database


You can do either. It is simply a string. You will have to set up the config
information, in the web.config, for that connection name. And, you can then
specify that particular connection, out of any number of connections, to
hit.

It would be something like this:

public Function GetDatabase(ByRef databaseName As String) as Database
Return DatabaseFactory.CreateDatabase(databaseName)
End Function

You can then pull from config like:

Dim dbName as String = ConfigurationManager.AppSettings("databaseName")

This would be like this in Web.Config

<add key="databaseName" value="MyFirstDatabase" />


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
"shapper" <mdmoura@gmail.comwrote in message
news:1182452083.236952.147240@n60g2000hse.googlegr oups.com...
Quote:
Hello,
>
I am using Microsoft Enterprise Library, on a class, to connect to a
database as follows:
>
Dim db As Database = DatabaseFactory.CreateDatabase("DatabaseName")
>
However, I would like the database name to be dynamic, i.e., specified
on the web site.
>
Can I define a connection string in my web site web.config and then
use it to create the database?
>
Or can I create my own Web.Config group to hold this information?
>
Thanks,
Miguel
>

David R. Longnecker
Guest
 
Posts: n/a
#3: Jun 21 '07

re: Connect to Database


Miguel-

Unfortunately, the Factory doesn't have an overload that takes a connection
string. To directly pass a connection string, you have to bypass the factory
and directly use the database objects.

For example, if you're using Microsoft.Practices.EnterpriseLibrary.Data,
you'd take that a step farther to .Sql or .Oracle, etc.

From there you could use

SqlDatabase db = new SqlDatabase("connectionString");

or, to make it more dynamic, pull in the database name from a variable, a
querystring, wherever, as long as it matches the entry in the web.config.

string databaseName = ""; // Get database name from wherever
it is coming from.
SqlDatabase db =
new SqlDatabase(
System.Configuration.ConfigurationManager.Connecti onStrings[databaseName].ConnectionString);

Finally, the method I use is to create a ConnectionStrings class and simply
return each... creating a somewhat pseudo enum for VERY common connection
strings that I don't want to modify in 30+ web.configs if something changes;
I just want to update the framework library and push it back out to the GAC
in a server. If interested in that, check the blog entry below:

http://tiredblogger.wordpress.com/20...se-library-30/

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

sHello,
s>
sI am using Microsoft Enterprise Library, on a class, to connect to a
sdatabase as follows:
s>
sDim db As Database = DatabaseFactory.CreateDatabase("DatabaseName")
s>
sHowever, I would like the database name to be dynamic, i.e.,
sspecified on the web site.
s>
sCan I define a connection string in my web site web.config and then
suse it to create the database?
s>
sOr can I create my own Web.Config group to hold this information?
s>
sThanks,
sMiguel


sloan
Guest
 
Posts: n/a
#4: Jun 21 '07

re: Connect to Database



I think this will work:



<add key="MyPreferredDatabase" value="StagingConnectionString" />



<connectionStrings>

<add name="DevelopmentConnectionString"
connectionString="server=MyDevelopmentServer;datab ase=Northwind;User
ID=northwinduser;password=northwindpassword"
providerName="System.Data.SqlClient"/>

<add name="ProductionConnectionString"
connectionString="server=MyProductionServer;databa se=Northwind;User
ID=northwinduser;password=northwindpassword"
providerName="System.Data.SqlClient"/>

<add name="StagingConnectionString"
connectionString="server=MyStagingServer;database= Northwind;User
ID=northwinduser;password=northwindpassword"
providerName="System.Data.SqlClient"/>



</connectionStrings>


-----------The Code

private Database GetADatabase ( )
{

string preferredDBName =
System.Configuration.ConfigurationManager["MyPreferredDatabase"];


Database db = DatabaseFactory.CreateDatabase(preferredDBName);

return db;


}




I think that is what Cowboy was getting at.

I don't think grabbing a concrete database (SqlDatabase) is a good solution.






"David R. Longnecker" <dlongnecker@community.nospamwrote in message
news:463c24757728c982276e775613@msnews.microsoft.c om...
Quote:
Miguel-
>
Unfortunately, the Factory doesn't have an overload that takes a
connection
Quote:
string. To directly pass a connection string, you have to bypass the
factory
Quote:
and directly use the database objects.
>
For example, if you're using Microsoft.Practices.EnterpriseLibrary.Data,
you'd take that a step farther to .Sql or .Oracle, etc.
>
From there you could use
>
SqlDatabase db = new SqlDatabase("connectionString");
>
or, to make it more dynamic, pull in the database name from a variable, a
querystring, wherever, as long as it matches the entry in the web.config.
>
string databaseName = ""; // Get database name from wherever
it is coming from.
SqlDatabase db =
new SqlDatabase(
>
System.Configuration.ConfigurationManager.Connecti onStrings[databaseName].Co
nnectionString);
Quote:
>
Finally, the method I use is to create a ConnectionStrings class and
simply
Quote:
return each... creating a somewhat pseudo enum for VERY common connection
strings that I don't want to modify in 30+ web.configs if something
changes;
Quote:
I just want to update the framework library and push it back out to the
GAC
Quote:
in a server. If interested in that, check the blog entry below:
>
>
http://tiredblogger.wordpress.com/20...se-library-30/
Quote:
>
HTH.
>
-dl
>
--
David R. Longnecker
http://blog.tiredstudent.com
>
sHello,
s>
sI am using Microsoft Enterprise Library, on a class, to connect to a
sdatabase as follows:
s>
sDim db As Database = DatabaseFactory.CreateDatabase("DatabaseName")
s>
sHowever, I would like the database name to be dynamic, i.e.,
sspecified on the web site.
s>
sCan I define a connection string in my web site web.config and then
suse it to create the database?
s>
sOr can I create my own Web.Config group to hold this information?
s>
sThanks,
sMiguel
>
>

David R. Longnecker
Guest
 
Posts: n/a
#5: Jun 25 '07

re: Connect to Database


That's a good point, Sloan.

A question though: While I realize the that bypassing the factory of the
EntLibs may not be clean, have you come across a best practice for directly
passing in connection strings without using an app/web.config file? What
I ran into, and the reason I directly use the Database objects, is that our
connection strings are anything but stable and for our framework libraries,
it made more sense to create class pseudo-enumerators and pass them along
rather than change 30+ app/web.config files throughout our enterprise. Make
a change to a library, reroll that library to the 2 servers those apps read
from, and be done. The apps are spread throughout the enterprise, so placing
them in the machine.config (if even possible) didn't even seem as clean of
a solution.

The solution you provide still requires manually editing, re-encrypting,
and rerolling config files to the application sources.

Thanks for the explaination^^.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

sI think this will work:
s>
s<add key="MyPreferredDatabase" value="StagingConnectionString" />
s>
s<connectionStrings>
s>
s<add name="DevelopmentConnectionString"
sconnectionString="server=MyDevelopmentServer;data base=Northwind;User
sID=northwinduser;password=northwindpassword"
sproviderName="System.Data.SqlClient"/>
s>
s<add name="ProductionConnectionString"
sconnectionString="server=MyProductionServer;datab ase=Northwind;User
sID=northwinduser;password=northwindpassword"
sproviderName="System.Data.SqlClient"/>
s>
s<add name="StagingConnectionString"
sconnectionString="server=MyStagingServer;database =Northwind;User
sID=northwinduser;password=northwindpassword"
sproviderName="System.Data.SqlClient"/>
s>
s</connectionStrings>
s>
s-----------The Code
s>
sprivate Database GetADatabase ( )
s{
sstring preferredDBName =
sSystem.Configuration.ConfigurationManager["MyPreferredDatabase"];
sDatabase db = DatabaseFactory.CreateDatabase(preferredDBName);
s>
sreturn db;
s>
s}
s>
sI think that is what Cowboy was getting at.
s>
sI don't think grabbing a concrete database (SqlDatabase) is a good
ssolution.
s>
s"David R. Longnecker" <dlongnecker@community.nospamwrote in message
snews:463c24757728c982276e775613@msnews.microsoft. com...
s>
Quote:
Quote:
>Miguel-
>>
>Unfortunately, the Factory doesn't have an overload that takes a
>>
sconnection
s>
Quote:
Quote:
>string. To directly pass a connection string, you have to bypass the
>>
sfactory
s>
Quote:
Quote:
>and directly use the database objects.
>>
>For example, if you're using
>Microsoft.Practices.EnterpriseLibrary.Data, you'd take that a step
>farther to .Sql or .Oracle, etc.
>>
>From there you could use
>>
>SqlDatabase db = new SqlDatabase("connectionString");
>>
>or, to make it more dynamic, pull in the database name from a
>variable, a querystring, wherever, as long as it matches the entry in
>the web.config.
>>
>string databaseName = ""; // Get database name from wherever
>it is coming from.
>SqlDatabase db =
>new SqlDatabase(
sSystem.Configuration.ConfigurationManager.Connect ionStrings[databaseN
same].Co nnectionString);
s>
Quote:
Quote:
>Finally, the method I use is to create a ConnectionStrings class and
>>
ssimply
s>
Quote:
Quote:
>return each... creating a somewhat pseudo enum for VERY common
>connection strings that I don't want to modify in 30+ web.configs if
>something
>>
schanges;
s>
Quote:
Quote:
>I just want to update the framework library and push it back out to
>the
>>
sGAC
s>
Quote:
Quote:
>in a server. If interested in that, check the blog entry below:
>>
shttp://tiredblogger.wordpress.com/20...onnection-stri
sngs-for-enterprise-library-30/
s>
Quote:
Quote:
>HTH.
>>
>-dl
>>
>--
>David R. Longnecker
>http://blog.tiredstudent.com
>sHello,
>s>
>sI am using Microsoft Enterprise Library, on a class, to connect to
>a
>sdatabase as follows:
>s>
>sDim db As Database =
>DatabaseFactory.CreateDatabase("DatabaseName")
>s>
>sHowever, I would like the database name to be dynamic, i.e.,
>sspecified on the web site.
>s>
>sCan I define a connection string in my web site web.config and
>then
>suse it to create the database?
>s>
>sOr can I create my own Web.Config group to hold this information?
>s>
>sThanks,
>sMiguel

Closed Thread