user710502
user710502

Reputation: 11471

Best way to set up the connection setting

I am new to windows forms programming and I was wondering what would be the best way to create like a connectionstring.config (i dont know how to do this) I have seens it , its like an xml giving all the information to connect to the database, in my case I am connecting to a MySql database. I would like to have so i could do something like this (I KNOW THIS IS WRONG BUT YOU GET MY IDEA):

MySqlConnection conn = new MySqlConnection
(Someconfiguration.thatconnects.toMyXMLOrSomething["MyXMLFile]);

something like that, i know i am probably too far from what it is.. but I have seen this somewhere and i think its clean, instead of putting the data connection information everywhere i need it.

so a few questions:

I would really appreciate all the possible help as I am learning and would like to keep everything separate and clean like this.

Thank you for your help and valuable time to help me.

Upvotes: 1

Views: 900

Answers (5)

Timeout
Timeout

Reputation: 7909

You can put them in a specific node inside your app.config file.

Here's the MSDN documentation for adding the app.config file to your project and it includes adding connection strings: http://msdn.microsoft.com/en-us/library/ms243192(v=vs.100).aspx

<configuration>
    <connectionStrings>
        <add name="myConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;" />
    </connectionStrings>
<configuration>

You can then use the built in .NET ConfigurationManager class to pull it out:

ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Upvotes: 2

Coding101
Coding101

Reputation: 531

Here is some Microsoft documentation on the topic.

http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ApplicationConnectionString" connectionString="YOUR_CONNECTION_STRING" providerName="System.Data.PROVIDER_NAME"/>
  </connectionStrings>
</configuration>

Upvotes: 0

Alex
Alex

Reputation: 35399

  • How do i create that xml file in VS2010?

Add a new app.config file in the root of your project.

  • Where do I place that file?

Normally the root of your application.

  • How should I call it in the functions where i am using it?

    string value = System.Configuration.ConfigurationManager.AppSettings[key]; string connection = System.Configuration.ConfigurationManager.ConnectionStrings[key];

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160852

Right click your project - Add -> New Item... - General - Application Configuration File (app.config). You can add a connectionStrings section there.

See "Connection Strings and Configuration Files" for details.

If you really want to put you connection strings in a separate file you can set this up as well within your app.config file:

<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <connectionStrings configSource="connections.config"/>
</configuration>

Upvotes: 1

Nick Strupat
Nick Strupat

Reputation: 5063

Put in in app.config. It should already be in your project.

Upvotes: 0

Related Questions