C# SQL connection string variable

c++, connection-string, sql, variables

In C# WinForms, where should I put my SQL connection string variable if I want to access it all over my application?

Right now I'm copy-pasting it wherever I'm using it.

//Sql connection stringSqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'");SqlCommand command = con.CreateCommand();

Best Solution

You can simply use the app.config (or web.config) for that. It has a special section for connection-strings. See the MSDN-article about that. Then you can retrieve the string in code as use1515791 has already pointed out, like this: