tips on declaring connection string globally - newbie

J

joel

Hi guys,

I am a newbie in programming and i wonder how to do this..
Everytime i code forms i have to declare the connection string

sample:

Private Sub frmItems_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlStrCon = "Data Source=Server1;Initial
Catalog=Inventory_DB;Integrated Security=True;User
ID=sa;password=1234"

SqlCon = New SqlConnection(SqlStrCon)

i thought this is wrong... because what if the server name changes and
i have 50 forms... i have to change every 1 of it.

how can i declare my connection string globally so that i dont have to
declare it everytime i code forms?

I hope i stated it clear...

any suggestion is very much appreciated guys!

joel
 
H

Henning Krause [MVP - Exchange]

Hello,

put it in you app.config file in the section <connectionStrings> like in
this example (taken from MSDN):

<configuration>
<!-- Other configuration settings -->

<connectionStrings>

<add name="Sales"
providerName="System.Data.SqlClient"
connectionString=
"server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts" />

<add name="NorthWind"
providerName="System.Data.SqlClient"
connectionString="server=.;database=NorthWind;Integrated
Security=SSPI" />

</connectionStrings>
</configuration>

You can now access the connection string via
ConfigurationManager.ConnectionStrings["Sales"].

To get access to the ConfigurationManager, you'll have to reference the
System.Configuration assembly.

Kind regards,
Henning Krause
 
A

Akhilesh

Hi guys,

I am a newbie in programming and i wonder how to do this..
Everytime i code forms i have to declare the connection string

sample:

Private Sub frmItems_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlStrCon = "Data Source=Server1;Initial
Catalog=Inventory_DB;Integrated Security=True;User
ID=sa;password=1234"
Put them in .cofig files.

ASP.NET = web.config
windows = app.config

to access u can use configurationsettings class in
System.Configuration namespace

-Akki
 
J

joel

Hello,

put it in you app.config file in the section <connectionStrings> like in
this example (taken from MSDN):

<configuration>
<!-- Other configuration settings -->

<connectionStrings>

<add name="Sales"
providerName="System.Data.SqlClient"
connectionString=
"server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts" />

<add name="NorthWind"
providerName="System.Data.SqlClient"
connectionString="server=.;database=NorthWind;Integrated
Security=SSPI" />

</connectionStrings>
</configuration>

You can now access the connection string via
ConfigurationManager.ConnectionStrings["Sales"].

To get access to the ConfigurationManager, you'll have to reference the
System.Configuration assembly.

Kind regards,
Henning Krause




I am a newbie in programming and i wonder how to do this..
Everytime i code forms i have to declare the connection string

Private Sub frmItems_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlStrCon = "Data Source=Server1;Initial
Catalog=Inventory_DB;Integrated Security=True;User
ID=sa;password=1234"
SqlCon = New SqlConnection(SqlStrCon)
i thought this is wrong... because what if the server name changes and
i have 50 forms... i have to change every 1 of it.
how can i declare my connection string globally so that i dont have to
declare it everytime i code forms?
I hope i stated it clear...
any suggestion is very much appreciated guys!
joel- Hide quoted text -

- Show quoted text -

Wow!

Guys you're great!

thanks for the tips!

Thanks guys! really appreciate it.

Kudos.

- joel
 
N

Niraj Doshi

Hi i went through your forum and in my application i have used this allready but can you tell me can i change my connection string in appconfig file during runtime, if than how
Hi guys,

I am a newbie in programming and i wonder how to do this..
Everytime i code forms i have to declare the connection string

sample:

Private Sub frmItems_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlStrCon = "Data Source=Server1;Initial
Catalog=Inventory_DB;Integrated Security=True;User
ID=sa;password=1234"

SqlCon = New SqlConnection(SqlStrCon)

i thought this is wrong... because what if the server name changes and
i have 50 forms... i have to change every 1 of it.

how can i declare my connection string globally so that i dont have to
declare it everytime i code forms?

I hope i stated it clear...

any suggestion is very much appreciated guys!

joel
On Wednesday, July 25, 2007 3:06 AM Henning Krause [MVP - Exchange] wrote:
Hello,

put it in you app.config file in the section <connectionStrings> like in
this example (taken from MSDN):

<configuration>
<!-- Other configuration settings -->

<connectionStrings>

<add name="Sales"
providerName="System.Data.SqlClient"
connectionString=
"server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts" />

<add name="NorthWind"
providerName="System.Data.SqlClient"
connectionString="server=.;database=NorthWind;Integrated
Security=SSPI" />

</connectionStrings>
</configuration>

You can now access the connection string via
ConfigurationManager.ConnectionStrings["Sales"].

To get access to the ConfigurationManager, you'll have to reference the
System.Configuration assembly.

Kind regards,
Henning Krause

news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top