Reputation: 3954
I have written a class library that uses Linq To Sql to talk to a SQL Database.
When I added the .dml file it automatically placed a connection string in my app.config file like this:
<connectionStrings>
<add name="core.Properties.Settings.TruePotentialConnectionString"
connectionString="Data Source=(local);Initial Catalog=thedatabase;Persist Security Info=True;User ID=sa;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
The class library works perfectly.
I have now moved on to write a Winforms UI that references this class library and I have just realised I have no idea how to change the value of the connection string when it is running on a different machine.
Ideally I would like to be able to add a connection string as a Setting in the UI that I pass to the dll to overwrite the one in the dll.
What is the correct way to change the connection string that the dll is using and not use the one in its app.config?
Upvotes: 0
Views: 862
Reputation: 8147
Your ConnectionString
value is pulled from the Hosts app.config
. In this instance your WinForms application is the host so if you copy your ConnectionString
into your WinForms app.config
it will then be used by the library.
Update:
If you wish to set the ConnectionString
dynamically during run-time then you can pass an override connection string into the DataContext
constructor like so:
var connectionString = "Data Source=MegaServer;Initial Catalog=MyDb; .. etc ..";
using (var db = new MyDataContext(connectionString))
{
// This will connect to MegaServer...
}
or to use the one from the WinForms app.config
just do:
using (var db = new MyDataContext())
{
// This will connect to (local) from app.config...
}
Upvotes: 1
Reputation: 13248
When you create a new instance of the DataContext
, one of the overloaded constructors accepts a connection string.
Place the connection string into the app.config file and it should work.
Make sure that you have an app.config in the application otherwise this will not work ie put it in the WinForms project.
Upvotes: 0
Reputation: 2895
You can do this in the App.config file (similar to your Web.config file) it has the same connectionStrings section. You just have to use the same name for the connection string that your DLL uses and it ought to pick it up.
Upvotes: 0
Reputation: 13960
You need to add a configuration file (app.config) to your application. Then, copy the settings from you dll app.config to the executable app.config file.
Upvotes: 0