473,804 Members | 3,708 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 4523
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(ByR ef databaseName As String) as Database
Return DatabaseFactory .CreateDatabase (databaseName)
End Function

You can then pull from config like:

Dim dbName as String = ConfigurationMa nager.AppSettin gs("databaseNam e")

This would be like this in Web.Config

<add key="databaseNa me" value="MyFirstD atabase" />
--
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.goo glegroups.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.Pract ices.Enterprise Library.Data,
you'd take that a step farther to .Sql or .Oracle, etc.

From there you could use

SqlDatabase db = new SqlDatabase("co nnectionString" );

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.Configur ation.Configura tionManager.Con nectionStrings[databaseName].ConnectionStri ng);

Finally, the method I use is to create a ConnectionStrin gs 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="MyPreferre dDatabase" value="StagingC onnectionString " />

<connectionStri ngs>

<add name="Developme ntConnectionStr ing"
connectionStrin g="server=MyDev elopmentServer; database=Northw ind;User
ID=northwinduse r;password=nort hwindpassword"
providerName="S ystem.Data.SqlC lient"/>

<add name="Productio nConnectionStri ng"
connectionStrin g="server=MyPro ductionServer;d atabase=Northwi nd;User
ID=northwinduse r;password=nort hwindpassword"
providerName="S ystem.Data.SqlC lient"/>

<add name="StagingCo nnectionString"
connectionStrin g="server=MySta gingServer;data base=Northwind; User
ID=northwinduse r;password=nort hwindpassword"
providerName="S ystem.Data.SqlC lient"/>

</connectionStrin gs>
-----------The Code

private Database GetADatabase ( )
{

string preferredDBName =
System.Configur ation.Configura tionManager["MyPreferredDat abase"];
Database db = DatabaseFactory .CreateDatabase (preferredDBNam e);

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*********@co mmunity.nospamw rote in message
news:46******** *************** *@msnews.micros oft.com...
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.Pract ices.Enterprise Library.Data,
you'd take that a step farther to .Sql or .Oracle, etc.

From there you could use

SqlDatabase db = new SqlDatabase("co nnectionString" );

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.Configur ation.Configura tionManager.Con nectionStrings[databaseName].Co
nnectionString) ;
>
Finally, the method I use is to create a ConnectionStrin gs 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="MyPreferre dDatabase" value="StagingC onnectionString " />
s>
s<connectionStr ings>
s>
s<add name="Developme ntConnectionStr ing"
sconnectionStri ng="server=MyDe velopmentServer ;database=North wind;User
sID=northwindus er;password=nor thwindpassword"
sproviderName=" System.Data.Sql Client"/>
s>
s<add name="Productio nConnectionStri ng"
sconnectionStri ng="server=MyPr oductionServer; database=Northw ind;User
sID=northwindus er;password=nor thwindpassword"
sproviderName=" System.Data.Sql Client"/>
s>
s<add name="StagingCo nnectionString"
sconnectionStri ng="server=MySt agingServer;dat abase=Northwind ;User
sID=northwindus er;password=nor thwindpassword"
sproviderName=" System.Data.Sql Client"/>
s>
s</connectionStrin gs>
s>
s-----------The Code
s>
sprivate Database GetADatabase ( )
s{
sstring preferredDBName =
sSystem.Configu ration.Configur ationManager["MyPreferredDat abase"];
sDatabase db = DatabaseFactory .CreateDatabase (preferredDBNam e);
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*********@co mmunity.nospamw rote in message
snews:46******* *************** **@msnews.micro soft.com...
s>
>Miguel-

Unfortunatel y, 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.Prac tices.Enterpris eLibrary.Data, you'd take that a step
farther to .Sql or .Oracle, etc.

From there you could use

SqlDatabase db = new SqlDatabase("co nnectionString" );

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.Configu ration.Configur ationManager.Co nnectionStrings[databaseN
same].Co nnectionString) ;
s>
>Finally, the method I use is to create a ConnectionStrin gs 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 =
DatabaseFactor y.CreateDatabas e("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
4828
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: " . mysql_error()); echo "Connected successfully"; mysql_close($link);
1
4819
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 window with three screen items for username, password and service name. And on run time after connecting to the database I use this window to reconnect to an other database. So when I give the appropriate information, I will connect to the
3
6069
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 checking of the data, tap will then insert the data to the informix database. I know we can point the tap to one specified port in Informix database through the services file. My question is how should i know to which port my tap program send out...
4
5608
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 SQLNET in ORACLE that would allow me to connect to a database on a remote box (either AIX or AS/400)? I have read that DB2 Connect appears to be the ticket for the Db2 server if it is on AS/400 or OS/390. What if it is on AIX? Does the same tool...
4
6478
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 define the servername / hostname in my Perl Progrem.. Here is the code:
3
4568
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 Developersdex http://www.developersdex.com ***
1
2048
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 the same physical network, and it has an MSAccess database which is used on the external webfarm as well as on the internal intranet. I can connect via the intranet because the DB file is on the same box from which the intranet is being served...
21
2348
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 database server B, I'm gettting 'login failed for user 'username' I've added the username to the database on server B, I can open up SMS and connect to the database that way, but I can't connect from my asp.net web app. any suggestions on what...
2
9804
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 server and client, so client could access the database through the connect server? like that: db2client <--db2 connect server <--db2 mainframe database
5
3131
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 database sample.
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.