Set Connection String

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

The following code allows me to get the connection string from app.config,

data_varable get_provider = new data_varable();
string m_connstr= "";
OleDbConnection cdata = new
OleDbConnection(get_provider.GetConnectionString(m_connstr));

conn.Open();
string m_connect = cdata.DataSource;

How do I set the connection string in app.config through my code?
 
The following code allows me to get the connection string from app.config,

data_varable get_provider = new data_varable();
string m_connstr= "";
OleDbConnection cdata = new
OleDbConnection(get_provider.GetConnectionString(m_connstr));

conn.Open();
string m_connect = cdata.DataSource;

How do I set the connection string in app.config through my code?

Does the data_varable class have a method to do it?

Arne
 
* Dave wrote, On 26-1-2010 2:24:
The following code allows me to get the connection string from app.config,

data_varable get_provider = new data_varable();
string m_connstr= "";
OleDbConnection cdata = new
OleDbConnection(get_provider.GetConnectionString(m_connstr));

conn.Open();
string m_connect = cdata.DataSource;

How do I set the connection string in app.config through my code?

You shouldn't. The app.Config should be deployed with th eapplication
and the standard deployment guidelines state that you deploy your
application in C:\Program Files\Vendor\AppName, where normal users will
not have permission to write.

Instead of using the Connectionstrings section in the config (which is
reserved for setting written once), you can read the value from the
Properties.Settings after you've added a setting for the
connectionstring to it (type of string) with a user scope (application
scope settings are again read-only).

Editing and saving the settings file is very straightforward:

Settings.Default.SomeProperty = "Some Value";
Settings.Default.Save(); // Saves settings in application configuration file
 
* Dave wrote, On 26-1-2010 2:24:

You shouldn't. The app.Config should be deployed with th eapplication
and the standard deployment guidelines state that you deploy your
application in C:\Program Files\Vendor\AppName, where normal users will
not have permission to write.

Instead of using the Connectionstrings section in the config (which is
reserved for setting written once), you can read the value from the
Properties.Settings after you've added a setting for the
connectionstring to it (type of string) with a user scope (application
scope settings are again read-only).

Editing and saving the settings file is very straightforward:

Settings.Default.SomeProperty = "Some Value";
Settings.Default.Save(); // Saves settings in application configuration
file

Hi Jesse,

I don't know what others may say, but I feel that saving a connection
string to the user config file in my opinion doesn't sound right.
Connection string is crucial to the application and is not belongs to
user modifiable settings. Usually I would make them part of the
installation process and if I have a need for the user to modify a
connection string, I will make sure the user have enough permission to
modify the file and do it through a ConfigurationManager.

The OP haven't given enough information for others to give a specific
advice (as Arne had pointed out, I'm wondering how data_varable is looks
like). If this part of the OP testing process, instead of modify a
connection string, he can always have as many connection strings as he
would like.

Regards.
 
* kndg wrote, On 27-1-2010 2:16:
Hi Jesse,

I don't know what others may say, but I feel that saving a connection
string to the user config file in my opinion doesn't sound right.
Connection string is crucial to the application and is not belongs to
user modifiable settings. Usually I would make them part of the
installation process and if I have a need for the user to modify a
connection string, I will make sure the user have enough permission to
modify the file and do it through a ConfigurationManager.

The OP haven't given enough information for others to give a specific
advice (as Arne had pointed out, I'm wondering how data_varable is looks
like). If this part of the OP testing process, instead of modify a
connection string, he can always have as many connection strings as he
would like.

Depends on how you want to use this application.

Say you have multiple instances of a database and a single application
to manage them, then you would want your user to be able to add/remove
connections as they go.

If you install the database with the application, I agree with you
completely.
 
Back
Top