My C# application use DataSet
and TableAdapters
. They are generated form VS2008 GUI Tool.
Example:
Right click project - > Add New Item - > DataSet
This method add a connection string automatically into app.config
.
But this is hard corded method for connection string. I want to change the connection string in a easy way. But when I used data set, then connection string get from application property. Are there any solution for this situation?
This is my connection string store in Settings.Designer.cs file
namespace WindowsFormsApplication2.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=SLCERT\\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True")]
public string TestDataBaseConnectionString {
get {
return ((string)(this["TestDataBaseConnectionString"])); // this is the connection string get from the dataset's
}
}
}
}
app.config contains
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication2.Properties.Settings.TestDataBaseConnectionString"
connectionString="Data Source=SLCERT\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I believe that you ask this, so that you don't have to manually change between local testing and production/test server.
You might want to look at How to: Transform Web.config When Deploying a Web Application Project
it's about web.config instead of app.config but it is the same idea.
ps. only for VS 2010 and above
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With