Edit parameters in Settings file
I have DataClasses.dbml file in my C# winforms project. This automatically adds a setting of type ConnectionString to Settings file of the project. The connection string throughout the project is accessed using this setting.
Now when I work on my PC, it connects to the database and works fine. But how to set a new connection string depending on client's host and instance names in the settings file permanently and for once (during setup).
I tried doing:
Settings.Default.ConnectionString = "SqlConnectionString";
Settings.Default.Save();
But it gives a compile-time er开发者_StackOverflowror that its Read-Only.
My only aim is to set the connectionstrings according the clients setting. I dont want to make it hard coded.
Add a partial
class definition like the following
public partial class DataClasses
{
partial void OnCreated()
{
Connection.ConnectionString = SQLHelpers.GetConnectionStr();
}
}
where SQLHelpers.GetConnectionStr
should lookup the settings from the users App.Config file.
Remember to put this in a separate file to your auto-generated dbml file.
精彩评论