2

I am using Winforms, MySQL and C# in my project. In that I use a connection string in the app settings.

At each new page I will declare a connection string as public and use this string in the connection.

MySqlConnection connection = new MySqlConnection(MyConString);

I want to declare this MyConString only one time in the whole application. How to do this? Where to do?

0

5 Answers 5

9

I don't think you should expose your connection string to your Forms, they don't need to know that. You can encapsulate the creation of connections with a simple factory.

public class ConnectionFactory
{
    public static MySqlConnection Create()
    {
        string connectionString = ConfigurationManager.AppSettings["..."];
        MySqlConnection conection = new MySqlConnection(Config.ConnectionStr);
        connection.Open();
        return connection;
    }
}

Then when you need a connection in a Form you can do:

private void button1_click(object sender, EventArg args)
{
    using ( var connection = ConnectionFactory.Create() )
    {
       connection.Execute("...");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can try something like the following:

public static class Config
{
    public static string ConnectionStr = ConfigurationManager.AppSettings["..."];
}

You can then use it in your code

MySqlConnection connection = new MySqlConnection(Config.ConnectionStr);

1 Comment

The Config class example needs to be added in a single place in the project. It probably should be in a file on its own.
2

Enterpise Library from Microsoft have great DataAccess part which beside other prtoblem solving and this one

Comments

2

You may have a separate class to handle databases and add the connection string as a field there. Each time you want to connect to the database, you may use that class. Also if you may use a property to access the string outside the class if you require.

Hope this helps...

Comments

2

The suggested approach is available at an MSDN article titled Storing and Retrieving Connection Strings. The following samples are slightly modified from this article.

After storing your connection string in an app.config file, you can retrieve all connection strings like so:

static void GetConnectionStrings()
{
    var settings = ConfigurationManager.ConnectionStrings;
    if (settings != null) {
        foreach(ConnectionStringSettings cs in settings) {
            Console.WriteLine(cs.Name);
            Console.WriteLine(cs.ProviderName);
            Console.WriteLine(cs.ConnectionString);
        }
    }
}

You could alternatively get the connection string by name:

// Returns null if the name is not found.
static string GetConnectionStringByName(string name)
{
    string returnValue = null;   // Assume failure.
    var settings = ConfigurationManager.ConnectionStrings[name];
    if (settings != null) {
        returnValue = settings.ConnectionString;
    }
    return returnValue;
}

This also gives you the ability of Securing Connection Strings so that your database username & password are not embedded into your application assembly in clear-text.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.