473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Web.config: connection string or connection key?

Can someone explain the difference of using these functions below? And
what is the advantage/disadvantage of each?

static public SqlConnection GetConnectionString(string
ConnectionStringName)
{
SqlConnection myConnection = new
SqlConnection(ConfigurationManager.ConnectionStrin gs[ConnectionStringName].ConnectionString);
return myConnection;
}

static public SqlConnection GetConnectionKey(string
ConnectionKey)
{
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings[ConnectionKey]);
return myConnection;
}

Rod

Aug 3 '07 #1
3 3811

The first is more structure than the second.

The first syntax is the new ASP.NET 2.0 style of storing connection
info and is made specifically for connections. It allows associating
connection strings and provider names with a named connection string.
This simplifies using the new provider factory model and programming
in a generic way so your code is not tied to a specific database. In
both examples you use SqlConnection directly but you can and most
likely should be using DbConnection instead and that is more easily
accomplished with the first configuration method than the second.

Also the first method allows for storing encrypted connection strings
whereas the second does not (at least not without custom encryption
programming).

Additionally, the first will support the new management tools for
editing the Web.config better than the second (again, because it's
more structured).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 03 Aug 2007 13:55:17 -0000, rl********@gmail.com wrote:
>Can someone explain the difference of using these functions below? And
what is the advantage/disadvantage of each?

static public SqlConnection GetConnectionString(string
ConnectionStringName)
{
SqlConnection myConnection = new
SqlConnection(ConfigurationManager.ConnectionStri ngs[ConnectionStringName].ConnectionString);
return myConnection;
}

static public SqlConnection GetConnectionKey(string
ConnectionKey)
{
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings[ConnectionKey]);
return myConnection;
}

Rod
Aug 3 '07 #2
Samuel, I don't understand how the first method is not tied to a
specific database if in both cases you can specify a connection name.
Can you give an example?

One more thing: if I wanted to use a more generic method called
GetDBConnection() what would be the best approach? I ask that because
they use the same string parameter.

Rod

On Aug 3, 11:44 am, Samuel R. Neff <samueln...@nomail.comwrote:
The first is more structure than the second.

The first syntax is the new ASP.NET 2.0 style of storing connection
info and is made specifically for connections. It allows associating
connection strings and provider names with a named connection string.
This simplifies using the new provider factory model and programming
in a generic way so your code is not tied to a specific database. In
both examples you use SqlConnection directly but you can and most
likely should be using DbConnection instead and that is more easily
accomplished with the first configuration method than the second.

Also the first method allows for storing encrypted connection strings
whereas the second does not (at least not without custom encryption
programming).

Additionally, the first will support the new management tools for
editing the Web.config better than the second (again, because it's
more structured).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 03 Aug 2007 13:55:17 -0000, rlueneb...@gmail.com wrote:
Can someone explain the difference of using these functions below? And
what is the advantage/disadvantage of each?
static public SqlConnection GetConnectionString(string
ConnectionStringName)
{
SqlConnection myConnection = new
SqlConnection(ConfigurationManager.ConnectionStrin gs[ConnectionStringName].ConnectionString);
return myConnection;
}
static public SqlConnection GetConnectionKey(string
ConnectionKey)
{
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings[ConnectionKey]);
return myConnection;
}
Rod

Aug 3 '07 #3

I'm sorry I wasn't clear when I said specific database I meant
database engine. ADO.NET simplifies writing code that works with
MSSQL, MSAccess, Oracle, and any other database engine with an ADO.NET
provider.

A more generic example would be

ConnectionStringSettings info =
ConfigurationManager.ConnectionStrings["mydb"];

DbProviderFactory factory =
DbProviderFactories.GetFactory(info.ProviderName)

DbConnection cnn = factory.CreateConnection();
cnn.ConnectionString = info.ConnectionString;
The key point is that if you switch from MSSQL to Oracle or even from
MSSQL to MSSQL with a 3rd party provider (there are some) then you
don't have to change your code, just your config.

Doesn't help you if you have dbms-specific sql, but it's a good start.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 03 Aug 2007 18:37:01 -0000, rl********@gmail.com wrote:
>Samuel, I don't understand how the first method is not tied to a
specific database if in both cases you can specify a connection name.
Can you give an example?

One more thing: if I wanted to use a more generic method called
GetDBConnection() what would be the best approach? I ask that because
they use the same string parameter.

Rod
Aug 3 '07 #4

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

Similar topics

8
by: Robert | last post by:
I have what I think should be a pretty common scenario in a corporate environment. I have a web app and three environments on three servers: Development, QA and Production. We develop in...
3
by: Tim Gallivan | last post by:
Hi all, I think read somewhere (but I can't find it ... note to self: must get new filing system ...) that there is a workaround so that an app.config can have multiple keys with the same name...
14
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review,...
5
by: Sridhar | last post by:
Hi, I have created a project which contains classes to read the data from the database. This project has an App.Config file which contains the SqlConnection String. when this code is called from...
8
by: theWizard1 | last post by:
Using Asp.NET 1.1, and C#. I have a directory for the website, and a directory under it named Secure. I have a web.config in each of the above directories. The web.config in the Secure...
2
by: malice | last post by:
I have been searching the internet for 12+ hours on how to do this. At this point I'm about to give up. I am using VB .Net 2.0 with the enterprise library jan 2006 release. This is my first time...
12
by: Ben | last post by:
I have a group of settings that I'd like to have inherited by multiple sites. I'm trying this, but it's not working. wwwroot\group\web.config wwwroot\group\site1\web.config...
10
by: eagle | last post by:
I have a web.config in my application that contains the connection strings to all my datasources. I want to move these connection strings to another web config up the folder hierarchy so that all...
2
by: bz | last post by:
Hi, I have a library project that implements a Business Layer for a web and a desktop application All my business classes are in this lib, so I have here the connection string to database as...
2
by: Johnson | last post by:
I'm trying to fix a "sub optimal" situation with respect to connection string management. Your thoughtful responses will be appreciated. I just started with a new client who has a bunch of legacy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.