I have just completed my first project in WPF using C# and MS SQL 2008 express. I have used Visual Studio 2010. What I have right now is a class `conn.cs' that has a method that returns me the connection string as and when I require. Also I just noticed that I have an App.Config file that also has a connection string defined there (both strings refer to the same database).
My conn.cs
class conn
{
public string get_connection()
{
string conn_string = @"Data Source=.\sqlexpress;Initial Catalog=msp;Integrated Security=True;Pooling=False";
return conn_string;
}
}
App.Config
<connectionStrings>
<add name="msp.Properties.Settings.mspConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=msp;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
I want to know two things now.
- How can I fetch the Connection String from the App.Config? I need to do this then, I will fetch the string in my
conn.csfrom there, and then, I will just change the connection string in the app.config as and when required. - Also tell me, is it possible to set connection string at run time? I want the user to browse to the database
(.mdf)file on First Run and then the connection string should get generated and saved inapp.config. I can then easily pick it up from there and use.
Please provide suggestions.