473,545 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to store connection string in congif file

Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..

--
Perin
Nov 18 '05 #1
6 1310
Hi Perin,
In web.config file, add the section below.

<configuratio n>
<appSettings>
<add key="Connection String"
value="server=l ocalhost;databa se=testdb;uid=s a;password=secr et;" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

In your code, access like this..

strConnection = ConfigurationSe ttings.AppSetti ngs("Connection String")
sqlConn = New SqlConnection(s trConnection)
sqlCmd = New SqlCommand("SEL ECT * FROM Customers", sqlConn)
sqlConn.Open()

Very simple..

Rgds,
John Paul. A

"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #2
This method is highlighted in the Microsoft Inovation Day

SQL Connection String in web.config using DPAPI:

http://msdn.microsoft.com/library/de...SecNetHT08.asp

http://builder.com.com/5100-6373_14-...ml#Listing%20A

"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it
from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #3
Leo
Just remember to add the following import statement at the top of any
module/Codebehind/class that will be using

ConfigurationSe ttings.AppSetti ngs("Connection String")
Imports System.Configur ation.Configura tionSettings

"John Paul. A" <Jo*******@disc ussions.microso ft.com> wrote in message
news:13******** *************** ***********@mic rosoft.com...
Hi Perin,
In web.config file, add the section below.

<configuratio n>
<appSettings>
<add key="Connection String"
value="server=l ocalhost;databa se=testdb;uid=s a;password=secr et;" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

In your code, access like this..

strConnection = ConfigurationSe ttings.AppSetti ngs("Connection String")
sqlConn = New SqlConnection(s trConnection)
sqlCmd = New SqlCommand("SEL ECT * FROM Customers", sqlConn)
sqlConn.Open()

Very simple..

Rgds,
John Paul. A

"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access
it
from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #4
In web.Config you have an <appSettings> section.
Insert following lines
<add key="CS"
value=" Write connection string here." />

From codebehind pages access CS by
Me.SQLConnectio n1.ConnectionSt ring =
System.Configur ation.Configura tionSettings.Ap pSettings("CS")

HTH.
Regards
Jignesh.

"Perin" <ja************ ****@hotmail.co m> wrote in message
news:9A******** *************** ***********@mic rosoft.com...
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can anyone
tell me, how to store my connection string in web.config file and access it from my application.
It is very urgent please..

--
Perin

Nov 18 '05 #5
Hello John,

Even better would be a class that wraps the ConfigurationSe ttings, so that
you arent relying on magic constants...

class MyConfiguration
{
public static string ConnectionStrin g { get { return ConfigurationSe ttings.AppSetti ngs["ConnectionStri ng"];
} }
}

then: SqlConnection conn = new SqlConnection(M yConfiguration. ConnectionStrin g);

This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.

--
Matt Berther
http://www.mattberther.com
Hi Perin,
In web.config file, add the section below.
<configuratio n>
<appSettings>
<add key="Connection String"
value="server=l ocalhost;databa se=testdb;uid=s a;password=secr et;"
/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
In your code, access like this..

strConnection = ConfigurationSe ttings.AppSetti ngs("Connection String")
sqlConn = New SqlConnection(s trConnection)
sqlCmd = New SqlCommand("SEL ECT * FROM Customers", sqlConn)
sqlConn.Open()
Very simple..

Rgds,
John Paul. A
"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can
anyone
tell me, how to store my connection string in web.config file and
access it
from my application.
It is very urgent please..
-- Perin

Nov 18 '05 #6
Dear MAtt Berther,
What you are saying is right.
Thanks for your guidance.

Rgds,
John Paul. A

"Matt Berther" wrote:
Hello John,

Even better would be a class that wraps the ConfigurationSe ttings, so that
you arent relying on magic constants...

class MyConfiguration
{
public static string ConnectionStrin g { get { return ConfigurationSe ttings.AppSetti ngs["ConnectionStri ng"];
} }
}

then: SqlConnection conn = new SqlConnection(M yConfiguration. ConnectionStrin g);

This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.

--
Matt Berther
http://www.mattberther.com
Hi Perin,
In web.config file, add the section below.
<configuratio n>
<appSettings>
<add key="Connection String"
value="server=l ocalhost;databa se=testdb;uid=s a;password=secr et;"
/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
In your code, access like this..

strConnection = ConfigurationSe ttings.AppSetti ngs("Connection String")
sqlConn = New SqlConnection(s trConnection)
sqlCmd = New SqlCommand("SEL ECT * FROM Customers", sqlConn)
sqlConn.Open()
Very simple..

Rgds,
John Paul. A
"Perin" wrote:
Hi,
I am using SQL Server as a Database in my ASP.NET application. Can
anyone
tell me, how to store my connection string in web.config file and
access it
from my application.
It is very urgent please..
-- Perin


Nov 18 '05 #7

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

Similar topics

12
7695
by: jacob nikom | last post by:
Hi, I would like to store XML files in MySQL. What is the best solution: 1. Convert it to string and store it as CLOB/text 2. Serialize it and store as byte array 3. Flatten it out and create one column per element, each column is VARCHAR Does MySQL has anything special for XML data? Is there any software which helps to store XML data...
4
2689
by: Steve | last post by:
C# I am trying to store my connection settings in a config file, mainly so I can easily change the db that the app connects to without doing a release of code. I have the config file sorted fine, and I can read the connection string in and connect. But at the moment everytime a connection is made to the database to get records, update...
4
2442
by: Alberto | last post by:
I have to store a connection string to a data base but I don't know where. Where do you prefer store it, in a text file or in a xml file? Thank you.
2
2271
by: Chris Dunaway | last post by:
I have a web service that references a data class library which performs SQL Server access. Since the web service is also a class library, there is no App.Config, only Web.config. Is Web.Config the place I should store the connection string? If I call System.Configuration.ConfigurationSettings.AppConfig from the data class library, will...
5
1290
by: Simon Harvey | last post by:
Hi everyone, As I understand it, storing an applications SQL Server connection string in the web.config file is a security risk. I'm wondering then, what the simplest solution is to this problem? Actually, my first question is, why is it a security risk? As I understand it, it is impossible to download the web.config file. Is this not the...
4
2417
by: Leo Muller | last post by:
I am about to deploy my first ASP.NET project. But, I was used to store my variables, such as connection strings, in the global.asa file, in an application. However, these variables are different on my development and production server. If I use the global.asax file, then it is in the code behing, compiled part. Thus I can't change the setting...
5
4497
by: Guadala Harry | last post by:
What are my options for *securely* storing/retrieving the ID and password used by an ASP.NET application for accessing a SQL Server (using SQL Server authentication)? Please note that this ID and password would be different than the one the user enters for ASP.NET forms authentication. The ID/password in question is used by the application,...
8
2552
by: Merk | last post by:
I'm looking for a safe and maintainable way to store connection string info (connecting to SQL Server 2005 from .NET 2.0 Windows Forms client app); things like server name or IP address and database name. I need to provide the client application with this info for connecting to both a test SQL Server and a production server. I would prefer...
2
1615
by: Man T | last post by:
I just wonder what is/are your practices to build the SQL Server connection string? Use object to build the connection string from config file or somewhere?
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7680
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. ...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7778
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...
0
6003
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
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...
0
3476
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...
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
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...

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.