Winforms accessing connectionstrings in multiple projects - c#

I have for example 3 Winforms projects inside the same solution.
Project1
Project2
Project3
When I build the solution, all the projects and his respectives files (.exe) are generated.
The problem is: Each project uses different connectionstring and just the solutions .config file is generated in the Bin folder.
When I deploy the solution to my customer, the project1 and project2 (for example) shows errors regarding to connectionstring problems (diferent Data Source, UserId and Password).
I've tried to put the connectionstring inside the .config file generated but it's not working too.
How can I manage these connectionstrings and works well when I deploy my programs ?
Regards
Jr

Each project needs to have its own connection string if it won't be connecting the same database/schema/or has same permissions on the DB. So you will have to change the name of the connection string for each project and place each of those connection strings in the config file of the built exe.
Then each project will be able to access its own connection string.

Add app.config to each project. You can store the connection strings specific to your project/assembly in this file. Then should be no problems.

Related

App.Config not found when running single project console app as an executable

When Running a console app packaged to a single .exe with costura.fody I appear to be unable to access my app.config resulting in the error
System.InvalidOperationException: No connection string named 'volumeEntities' could be found in the application config file.
at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
When I test in my nunit test project the app.config I added there which contains the connection string works.
I have an app.config added to the only other project in the solution.
It contains the correct connection string
In properties the Startup Object is set to 'Uploader.Program'
Am I missing some way of setting the project as the start up rather than the program class? How can I get the executable to find the app.config?
Fody costura does not package the app.config file with the executable so that you can change information in the app.config without recompiling. Copy the .exe.config to the same folder as the executable.

Entity Framework 6 connection string dll app config

I have 2 projects: Console Application and Class Library (dll)
My main project, the console app, references my dll.
In my dll I created an EF (Entity Framework) ADO model.
The EF connection string must be in the dll project .config file.
The problem is when I initialize a DbContext it is looking for the connection string in the Console Application .config file.
How can I tell the DbContext to look for the connection string in the dll .config file ?
Thanks
That's how config files work. The running app always looks in {appname}.exe.config. The config file for the DLL project is just there to give you placeholders that must be copied to the running app's config file. It is not deployed along with the DLL.
You should specify your connection string in your console app. Here is a quick example why you want to do that.
Imagine if you have another app that uses your DLL in your solution. What if it wants to connect to another database instance? Now you have to change your DLL connection string? But that would break your first app.
Very often you have different instances of you app running in different environments (e.g. development, staging, production). Each instance needs to connect to a different database. How would you reconfigure DLL connection string for each instance?
If somebody else uses your DLL, how can you know what database they want to point it to?
And there are a lot more arguments to why you really want to do it. All the tools are built around it too.
I believe Visual Studio doesn't add DLL config files when it compiles sources for apps that use these DLLs.
Hosting environments (e.g. Azure) allows you to easily change your web.config files, but don't know anything about your DLL config files.

what should be included on the app.config file of executable referencing other exe containing an app.config file

I am confused about how the app config file is used. I have a program that connects to a SQL server database , retrieves data from a specific table. I am using LinqToSql classes to do this. When I follow the wizard at the end the connection string gets placed in the app.config file.
I then plan to use that program as a class library so I add a reference to that exe from a different executable which is a wpf application. On that wpf application I have to place an app.config file containing the connection string in order for the program to work.
So far I understand everything. Now the part I am confused is why I do not have to copy also the settings located in the app.config file as well in order for the program to work? which settings are OK to be on the referenced executable and which ones are not. For example I know that the connection string should be on the app.config file of the wpf application. But the user settings :
app.config:
can be on the app.config of the referenced executable.
In summary why did I have to move content from the referenced executable's app.config file to the app.config file of the wpf application and I did not had to move other content from the referenced executable app.config file?
By default, the config file that is used is that of the executing process/program. Since your WPF app runs as its own executable, the config settings need to exist in its config file.
As for the other settings, your WPF app probably doesn't need them.
As a side note, it's usually a better practice to have a WPF/client app hit a service instead of going directly to the database itself. That would mean that the connection string would go in the config file of the service at that point. And the WPF app would just ask the service for the information.

change database connectionstring in class library project

I am using more than one projects in a solution, on e of them are real projects, others are helper projects. One of those helper projects, I have settings.settings file for storing database connection string. Now When I build/debug it. I don't see any file where I can change the connection string. Was it gone integrated in the helperproject's dll file? How can i resolve this os that i can change the connection string please?
The Settings class generated by the designer has "internal" access specifier by default.
So the application settings you've added will not be visible in the assembly that is referring the helper assembly.
To make it visible across the referring assemblies, make the class public manually or via the designer. See the snapshot.
After you've made it public, you can access it in other projects where this assembly is referred.
String connectionString = TestAssembly.Settings.Default.ConnectionString;
But it would be better to put the settings in a application configuration file specific to that application. In your case add an app.config to the main project; and add connectionStrings section in the config.
I think that you miss understood something. You should add at any project you want the app.config file (or web.config in case of web app / site).
Then via ConfigurationManager class you should access the data in it.
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
The app.config is automatically copied once you run/compile your project and it will have a name as youreapp.exe.config or yourelib.dll.config.
For more details read here http://www.codeproject.com/KB/files/SaveConnStringInAppConfig.aspx
Hope this helps

Share a Connection string between multiple web projects

OK I have two web projects WebProject1 & WebProject2. Both require database connectivity so this is all in a C#.NET project called Common.
Now my question is currently the connection string for both is the same and at the moment it's hard coded into the DB class (In the Common project), but I want to move it out to a config file.
I would really like to have a config file in the Common project with the connection string in which both web projects then use. Is this possible, and if so how?
There's a few ways you could do it:
put common configuration settings in machine.config as shown here
put common configuration settings in a central file and link to that in each projects's app.config as shown here
store the configuration settings in the registry
Here's a way to do this.
Seperate your DataLayer that interacts with the database by making it a class library project. The class library project will produce an assembly which you can further refer to any project you want. In this project add 'app.config' in the project-root and store your connection string into the app.config. Your data-access classes in the projects can then refer to the connection string in the app.config. When you compile and deploy your data-access project into an assembly the app.config is embeded into the assembly. Now you can add this assembly as reference to as many as projects you want to share the connection and data-access.

Categories

Resources