0

Where would you recommend hiding or writing a connection string in a winforms desktop app? I have a DAL (data access layer) where I hardcoded it for the time being, but I know it shouldn't be there. Any tips on this? (I'm in VS 2017.)

  • 2
    put it inside the app.config file – Ehsan Sajjad Sep 28 '17 at 13:13
  • 1
    You can use a configuration file. Please refer to this [link](https://stackoverflow.com/questions/114527/simplest-way-to-have-a-configuration-file-in-a-windows-forms-c-sharp-application) – Nythiennzo Sep 28 '17 at 13:14

1 Answers1

1

A Resource file should work well for this.

Using Visual Studio, you can add one to a project by right-clicking the project and choosing "Properties", then go to the "Resources" tab and either create a default resource file (by clicking the link), or updating the existing resource file using the UI.

Once your project has a default resource file with key-value pairs in it, using values from the resource file in code is as simple as

Properties.Resources.AnyKeyYouDefinedInTheResourceFile

...where AnyKeyYouDefinedInTheResourceFile is a key in the project's default resource file.

Note that the above example assumes that the code snippet is in the default namespace, since the full namespace for Properties is really WhateverYourDefaultNamespaceIsCalled.Properties.

bsinky
  • 515
  • 5
  • 15