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.
5 Solutions collect form web for “How to change the connection string on another computer?”
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:
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.
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
PS : use the write when you want to save the CNX String like in the save button of that form .