473,498 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Connect to Database

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

Jun 21 '07 #1
4 4504
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" <md*****@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
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

Jun 21 '07 #2
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
Jun 21 '07 #3

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" <dl*********@community.nospamwrote in message
news:46************************@msnews.microsoft.c om...
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].Co
nnectionString);
>
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


Jun 21 '07 #4
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" <dl*********@community.nospamwrote in message
snews:46************************@msnews.microsoft. com...
s>
>Miguel-

Unfortunately, the Factory doesn't have an overload that takes a
sconnection
s>
>string. To directly pass a connection string, you have to bypass the
sfactory
s>
>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>
>Finally, the method I use is to create a ConnectionStrings class and
ssimply
s>
>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>
>I just want to update the framework library and push it back out to
the
sGAC
s>
>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>
>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

Jun 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4774
by: Mr Dygi | last post by:
Hi, PHP 4.3.4 installed manually from package *.zip and Apache 2. I have a problem with this simple code: <?php $link = mysql_connect("127.0.0.1","","") or die("Could not connect: " ....
1
4755
by: DAVID | last post by:
Hello, With regards to the Connect dialog on Oracle forms 6i via which we can connect to Oracle database, should we use the same functionality on forms run time again? I mean, I want to make a...
3
6043
by: Lee | last post by:
Hi, I'm developing a socket program to connect to Informix database through the ODBC. In here i called my socket program as "tap" . My tap will listen for data from unix through port 1070. After...
4
5570
by: Scott Holland | last post by:
HELP - Need to connect to DB2 database on AIX from NT server. Also AS/400 from NT Server -- I am experienced in ORACLE and a novice at DB2. What tools would be the equivalent of Net*8 or...
4
6433
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
3
4552
by: Jassim Rahma | last post by:
I would like to know what is the best way to onnect to connect to a database in general which provides you with full functionality & fast access? Best Regards, Jassim Rahma *** Sent via...
1
2031
by: GNoter | last post by:
Scenario: I've a WebFarm with 2 web servers which are NLBs (network load balanced). Web1 and Web2; they are not part of a domain. I have a third server, Server3, which is part of a domain and on...
21
2298
by: Steve | last post by:
I moved my database from one server to another SQL server. I did a backup and restore of the database. I can connect to the database on server A from my asp.net app but when I try to connect to my...
2
9762
by: jeffhan | last post by:
we have os/400 db2 database at the backend. i installed db2 v9 connect server on one windows server which is locating the same network with database server. now how to i configure the connect...
5
3115
by: danfan46 | last post by:
Hi! I have a previous post on the subject that connect takes a long time. I uninstalled db2 completely. Installed V9.5 Installed fixpack 1 created das created an instance installed...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.