Home > Software design >  Is it possible to make connection string dynamically change whenever a C# application is installed i
Is it possible to make connection string dynamically change whenever a C# application is installed i

Time:11-26

I just deployed the project I created and when I shared the setup file and ran the application in another computer the database did not work. I think I know what the problem is, but I do not have the slightest idea on how to fix it because I am new to SQL.

I think the problem is the connection string:

        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=X:\Documents\SMS.mdf;Integrated Security=True;Connect Timeout=30");

Can someone tell me how fix it please? the application is a project for my course. Any help will do, thanks.

CodePudding user response:

Based on your question title, yes We can get different connection strings. All you need to do is define the connection string in some config file like App.Config or Web.Config or AppSettings.Json (Depending on the kind of application and target .net framework version).

and then read the respective configuration based on the environment or machine.

Refer these https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configuration?view=dotnet-plat-ext-7.0

https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-app-config-file?view=vs-2022

  • Related