Connection Strings modification after the deployment - c#

I created the setup project for the application and I can see that the later modifications of the configuration file (Application.exe.config) don't affect the application execution.
I am developing an application with the database file included and I want to enable users to move the database file and modify connection strings.
Does anyone know what's the best practice for the deployment of the application with the database file?

It should work, provided that you use the exact same connection string setting in your DB access DLL's Settings.settings file and in your application's config file.
An example that works well for me:
<connectionStrings>
<add name="YourApp.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=localhost;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
When entered appropriately in both locations (ie. the dll's Settings.settings and the exe's App.config files), this does allow me to change the database connection in YourApp.exe.config before the app runs.
(I assume you already know that you need to change the application's config file, as DLL's do not support the app.config mechanism directly.)

Have you checked out using a UDL file?
If you save your connection string in the udl file, the user can change the connection via an interface by simply double clicking that file.
You can set your connection string in the app to point to the udl file.
You can also launch the udl interface programmatically if you want.
The only downside to these is if the user saves their password to the file, it is saved as plain text. But this isn't an issue if you are using windows authentication.

did you make sure to remove the settings default values? These are compiled and fetched from the dll and not from the config file.

Related

How to open specific .config file in multi project solution?

Solution that I am working on is made of number of projects, one of them is start-up project(Windows Forms, .exe project) and has app.config file tied to it. At least one of the project(all other projects are .DLLs), that deals with database, will also needs to read app.config settings (to read db connection string). I want to centralize all of the application settings in one app.config file.
My questions are:
From what I understood, application configuration files are created per projects, not per solution ?
To access the app.config from DLLs should I use ConfigurationManager.OpenExeConfiguration(string exePath), where string exePath is the location of .exe for start-up project ?
Config files are connected to application domains, not DLLs. You can access your appication's (let's say web application or console application) configuration directly with ConfigurationManager class (OpenExeConfiguration not required).
If you need different connection strings in different part's of the application, you can add multiple connectionstrings in your configuration
<connectionStrings>
<add name="Connection1" connectionString="Data Source=..." />
<add name="Connection2" connectionString="Data Source=..." />
</connectionStrings>
and access them by name:
var connectionstring1 = ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString;
var connectionstring2 = ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString;
Visual Studio creates an app.config for each project, but only to provide a place to store configuration items for that assembly (since it could be used by multiple executing assemblies). Those configuration items should be incorporated into the app.config of the executing assembly.
You can add code to pull from multiple config files, but it's cleaner just to put them all in one app.config for the executable.
You need to copy all the project specific configuration (like connections strings etc.) into the the main app.config.

How to change the connection string on another computer?

I am very new to SQL and tried to build a software by using SQLServer and Visual Studio. I created a setup file for my program by using InstallShield Limited Edition Project. I want my program to be used on other computers and I want every user to be able use their own databases installed on their computers. To me, when a user installed the program, the program will search for a connection string that I used while creating the program. Therefore, I think this connection string must be changed by users. How can I add such properties into my program? By the way, I used model first entity framework in my program. My connection string written in app.config is :
<connectionStrings>
<add name="otobusVTNesneleri" connectionString="metadata=res://*/OtobusVeriModeli.csdl|res://*/OtobusVeriModeli.ssdl|res://*/OtobusVeriModeli.msl;provider=System.Data.SqlClient;provider connection string="data source=PIPASO\PIPASOSERVER;initial catalog=otobus;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
P.S. I searched the internet about this problem and saw that people suggest that I use SQL Server compact. I want to solve this problem without using it. I have SQL Server 2012.
You shouldinstall the SQL Server with the same instance name in all the PCs. And then, in the connection string, instead of specifying the computer name, use the "local" syntax:
Data Source=(local)\yourInstanceName
It's not a good idea to let your users change the connection string.
Entity Framework starting from version 6 supports Code Based configuration. Refer to this article: Code-Based Configuration (EF6 onwards)
without use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=Trainee4-PC;Initial Catalog=Demo05-09-2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=DENISH\SA;Initial Catalog=Demo05-09-2014;User ID=Demo;Password=Demo123" />
</connectionStrings>
</configuration>
local databases should be sql compact (sdf files). in any case, what you need to do is to make the installer modify the app.config file after it's deployed. this is what i'm doing in my current project. in my current project i have a local database (an sdf file) and an installer (created using WiX) that deploys my app. i configured the installer to modify some app.config settings after deploying the app. one of the settings was the connection to the local sdf database.
one thing to be careful about is that, while using sdf files, you cannot specify relative file paths in the config file. you need to either specify the full path (ie: d:\..\db.sdf) or use special folder names (ie |AppData|db.sdf). AppData will resolve to the current path of the executable.
requiring a full blown sql database on the client will also mean you'll have to install SQL Express on the client machine. you don't have to do this with sdf files.
in case you decide to use WiX you can modify your connection string like below.
<util:XmlFile Id="WindowsServiceUpdateConnectionString" File="[INSTALLFOLDER]$(var.Phoenix.WindowsService.TargetFileName).config" Action="setValue"
ElementPath= "//configuration/connectionStrings/add[\[]#name="PhoenixCacheEntities"[\]]/#connectionString"
Value="Data Source=[INSTALLFOLDER]Cache\PhoenixLocal.sdf"/>
you can see that an xpath is used here to determine the setting you want to change. hope this helps.
NOT RECOMANDED
its just becouse you said you are just starting in c#
so to write the connection string into a file :
using System.IO;//delcare it the top
File.Write("config.cfg","your connection string goes here");
to read From it :
using System.IO;//delcare it the top
string CNXSTRING = File.Read("config.cfg");
btw you have to create a form where the user and can type the new connection string lets say it have a textbox named txtstring the code will become
File.Write("config.cfg",txtstring.Text);
PS : use the write when you want to save the CNX String like in the save button of that form .

How to deploy multiple projects for an MVC website with different property settings files

I have an MVC website that also references another Class project in my solution called DAL
The MVC website has a settings.settings file that I am using for my Default database connection string, and this automatically gets updated in my Web.Config file.
However my Class project also uses a settings.settings file for a database connection (to create a connection if one is not already supplied to it by the MVC project) and I believe this is put into the App.Config file, but when it comes to deploying and running this MVC website?
What do I do for the DAL project?
I can see it has a DAL\bin\release\DAL.dll.config file in that location, does this need to go into the root of the website directory, using the same file path? or should I be doing something else?
Thanks
You should be able to override this setting in your web.config by using the full name. If your project is called DAL you should create a connection string in your web.config which looks something like.
<add name="DAL.Properties.Settings.MyDatabaseConnectionString"
connectionString="Data Source=(local);Initial Catalog=myDatabase;Integrated Security=true"
providerName="System.Data.SqlClient" />

Unable to find where my database file and connection string is located?

i downloaded a demo application from http://code.msdn.microsoft.com/Authorization-based-aa2c0185/view/SourceCode
I run it on my Visual Studio and all worked fine. There is a database ASPNETDB.MDF in its App_Data folder which is used to store the user information or retrieve information for login purpose.
Now what I want is to change this database with another database in my SQL Server.
For that there can be two ways :
Locate the connection string etc. for ASPNETDB and change them with my own database.
or add this ASPNETDB file to SQL Server by attaching. But its showing error while attaching it.
Also if I exclude App_Data folder from project...Then even it works...
Where can find connection string for DB? I want to change the DB with mine own ?
Please Help
Thanks
It's using default connection string from machine.config:
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
Go to the Web.debug.config file. There you will find comments noting how to wire up your config file. This will use config transforms so that when you build in debug mode, it will change your config file to override the machine defaults (which is what it is currently using). Just follow the comments and you should be good :)

What is the best way to store/set configuration settings for a C# application?

I have some values that I want to be able to set, and the application to load them from some kind of file.
The only concept I can think of, is a simple txt file, that might have the following lines:
DatabaseName = "DB1/test"
DatabasePassword = "password"
Development = "true"
but im thinking it should be in some kind of config file? Plus reading a txt file for these values isnt exactly tidy code. It would be nice if i could get the database name by just saying in my application:
configfile.DatabaseName
Thanks,
Paul
You really should be using the built in Application Settings
You can directly access simple settings using the ConfigurationManager
ConfigurationManager.AppSettings["MySetting"] = "SomeStuff";
var mySetting = ConfigurationManager.AppSettings["MySetting"];
There is also direct access to your Connection Strings using the ConfigurationManager
var conn = ConfigurationManager.ConnectionStrings["DevSqlServer"];
All this is stored in XML files, and by default your *.config files.
To Answer Doomsknight's question from the comments
Configuration settings can be done a number of ways, but by default, they are stored in two places.
Application Level Settings are stored in a configuration file.
For executable programs this file is located in the same directory as the .exe and is named after the assembly, or executable.
Example: MyAssembly.config, Another.Assembly.config
For web applications, the settings are stored in the web.config file (usually) located in the root directory of the web application. These are applied hierarchically and one can be located at each directory level of the Web Application.
Example: MySite\web.config, MySite\SubDirectory\web.config
User Scoped Settings are stored in the user profile
Example: C:\Documents and Settings\USERNAME\Local Settings\Application Data\ApplicationName
Connection Strings are stored in the <connectionStrings></connectionStrings> section in your config file.
<connectionStrings>
<clear />
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
These settings can easily be modified directly in the config file, but without writing some code to automatically refresh sections of the config file (which is possible), an application restart is typically needed.
I hope this helps out.
.NET has a configuration platform built into it. Just add an app.config file to your project and use the ConfigurationManager library to access the values.

Categories