![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
"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] |
#5
| |||
| |||
|
| "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? |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |