HighTechTalks DotNet Forums  

C# - Change connection string at runtime

Dotnet Framework (WinForms DataBinding) microsoft.public.dotnet.framework.windowsforms.databinding


Discuss C# - Change connection string at runtime in the Dotnet Framework (WinForms DataBinding) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
jeremy42
 
Posts: n/a

Default C# - Change connection string at runtime - 11-01-2008 , 09:42 PM






I am new to the .Net environment. I created a C# app that I want to
distribute and stored the ConnectionString in the configuration file. I
refer to it using ConfigurationManager whenever I need it. The data source
will be different for each user. How can I use code to change the
connectionstring at runtime?

I am frustrated and desperate for an answer.

Thank you for helping!
--
Jeremy42

Reply With Quote
  #2  
Old   
Morten Wennevik [C# MVP]
 
Posts: n/a

Default RE: C# - Change connection string at runtime - 11-24-2008 , 01:07 AM







"jeremy42" wrote:

Quote:
I am new to the .Net environment. I created a C# app that I want to
distribute and stored the ConnectionString in the configuration file. I
refer to it using ConfigurationManager whenever I need it. The data source
will be different for each user. How can I use code to change the
connectionstring at runtime?

I am frustrated and desperate for an answer.

Thank you for helping!
--
Jeremy42
Hi Jeremy,

Take a look at the Settings file (you may find a default Settings file in
the Properties folder). It wraps the app.config/web.config/user.config files
and is far easier to work with than using a ConfigurationManager. It even
has type safety and includes a ConnectionString type that maps the
ConnectionStrings section in the configuration file. However, the
ConnectionString type is available as Application scope only, which means you
can't change it at run time. Your scenario fits the user scope so you may
need to create a user settings of type string. You can then access this
setting using

string connectionString = Properties.Settings.Default.UserConnectionString;

To save it use

Properties.Settings.Default.UserConnectionString = "";
Properties.Settings.Default.Save();

--
Happy Coding!
Morten Wennevik [C# MVP]


Reply With Quote
  #3  
Old   
Morten Wennevik [C# MVP]
 
Posts: n/a

Default RE: C# - Change connection string at runtime - 11-24-2008 , 01:07 AM




"jeremy42" wrote:

Quote:
I am new to the .Net environment. I created a C# app that I want to
distribute and stored the ConnectionString in the configuration file. I
refer to it using ConfigurationManager whenever I need it. The data source
will be different for each user. How can I use code to change the
connectionstring at runtime?

I am frustrated and desperate for an answer.

Thank you for helping!
--
Jeremy42
Hi Jeremy,

Take a look at the Settings file (you may find a default Settings file in
the Properties folder). It wraps the app.config/web.config/user.config files
and is far easier to work with than using a ConfigurationManager. It even
has type safety and includes a ConnectionString type that maps the
ConnectionStrings section in the configuration file. However, the
ConnectionString type is available as Application scope only, which means you
can't change it at run time. Your scenario fits the user scope so you may
need to create a user settings of type string. You can then access this
setting using

string connectionString = Properties.Settings.Default.UserConnectionString;

To save it use

Properties.Settings.Default.UserConnectionString = "";
Properties.Settings.Default.Save();

--
Happy Coding!
Morten Wennevik [C# MVP]


Reply With Quote
  #4  
Old   
cpepper
 
Posts: n/a

Default RE: C# - Change connection string at runtime - 01-30-2009 , 12:12 PM





"Morten Wennevik [C# MVP]" wrote:

Quote:
"jeremy42" wrote:

I am new to the .Net environment. I created a C# app that I want to
distribute and stored the ConnectionString in the configuration file. I
refer to it using ConfigurationManager whenever I need it. The data source
will be different for each user. How can I use code to change the
connectionstring at runtime?

I am frustrated and desperate for an answer.

Thank you for helping!
--
Jeremy42

Hi Jeremy,

Take a look at the Settings file (you may find a default Settings file in
the Properties folder). It wraps the app.config/web.config/user.config files
and is far easier to work with than using a ConfigurationManager. It even
has type safety and includes a ConnectionString type that maps the
ConnectionStrings section in the configuration file. However, the
ConnectionString type is available as Application scope only, which means you
can't change it at run time. Your scenario fits the user scope so you may
need to create a user settings of type string. You can then access this
setting using

string connectionString = Properties.Settings.Default.UserConnectionString;

To save it use

Properties.Settings.Default.UserConnectionString = "";
Properties.Settings.Default.Save();

--
Happy Coding!
Morten Wennevik [C# MVP]
It does not work because you cannot assign to
Properties.Settings.Default.UserConnectionString .

What else can we try?


Reply With Quote
  #5  
Old   
Morten Wennevik [C# MVP]
 
Posts: n/a

Default RE: C# - Change connection string at runtime - 01-31-2009 , 05:43 AM




"cpepper" wrote:

Quote:

"Morten Wennevik [C# MVP]" wrote:


"jeremy42" wrote:

I am new to the .Net environment. I created a C# app that I want to
distribute and stored the ConnectionString in the configuration file. I
refer to it using ConfigurationManager whenever I need it. The data source
will be different for each user. How can I use code to change the
connectionstring at runtime?

I am frustrated and desperate for an answer.

Thank you for helping!
--
Jeremy42

Hi Jeremy,

Take a look at the Settings file (you may find a default Settings file in
the Properties folder). It wraps the app.config/web.config/user.config files
and is far easier to work with than using a ConfigurationManager. It even
has type safety and includes a ConnectionString type that maps the
ConnectionStrings section in the configuration file. However, the
ConnectionString type is available as Application scope only, which means you
can't change it at run time. Your scenario fits the user scope so you may
need to create a user settings of type string. You can then access this
setting using

string connectionString = Properties.Settings.Default.UserConnectionString;

To save it use

Properties.Settings.Default.UserConnectionString = "";
Properties.Settings.Default.Save();

--
Happy Coding!
Morten Wennevik [C# MVP]

It does not work because you cannot assign to
Properties.Settings.Default.UserConnectionString .

What else can we try?
Ah, it seems like the ConnectionString type is readonly. However, you can
simply use a string type instead.

--
Happy Coding!
Morten Wennevik [C# MVP]


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.