Change the Connecting string of the C# windows form application to it in another computer
I have a C# windows form application and I connect it to the SQL server in my computer. Now I going to deliver the software to a user. So, what can I do to change the connection string to the user SQL server? Is there any way to do the connection string computer independent? Kindly help me
4 Solutions collect form web for “Change the Connecting string of the C# windows form application to it in another computer”
I’m assuming you’re hard-coding connection strings into your code. You need an application config file.
Or you could use connection string like this (if database (SQL server) is on same machine as app using it and database name is the same) :
instanceName could be “nothing” (default instance) or named instance (SQLEXPRESS).
Or do it as @Xavier J suggested – store connection string in app config or INI file.
application config files are one idea, but because they are managed through the IDE means have to change manually for deployment.
Better to use the registry (cleaner too – no need to post-edit the file), and the installer can get conditional on where it’s deployed, alternatively ask the user during installation, skip if the registry entry already exists.), confirm the connection string at install time. database path/host/name exists…
You can store it in the app.config of your project. Then if you wanted the user to give credentials you could make a form and save the string to the value of that configuration.
<appSettings> <add key="connectionString" value="Connection string goes here"/> </appSettings>
Then call it and set it using the ConfigurationManager
ConfigurationManager.AppSettings["connectionString"] = "Your Value";
Or you could just replace the connection string yourself in the file once its on the users computer