1

Can we Place Database Connection Strings in .resx file in C# Class library project type?? If so, What are the advantages and disadvantages?

navule
  • 3,212
  • 2
  • 36
  • 54
  • 3
    What keeps you from putting it into the App.config / Web.config file? – Jens H Jun 27 '12 at 14:25
  • Sensitive data should of course be encrypted before putting them into the config file... – Jens H Jun 27 '12 at 14:29
  • I tried to put it in the app.config file but the problem is well explained here: [link]http://stackoverflow.com/q/11192842/1444246 and I am unable to retrieve it from web.config in UI layer to Repository(DAL). – navule Jun 27 '12 at 14:43

4 Answers4

3

You can put anything you want into a resx file and read it back, there should not be any problem with that. As far as advantages go, you do not get any advantage from it compared to putting it into your application config file. On the contrary, accessing the string becomes more cumbersome, and you cannot alter them at runtime.

Generally speaking, I see little difference between putting the connection string into resx and compiling it into your code with a compile-time constant.

It goes without saying that strings with passwords in them should never go into config or resx files as plain text.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • "It goes without saying that strings with passwords in them should never go into config or resx files" - I disagree regarding config files. In some (not all) cases it's appropriate - and a good idea to encrypt it using Protected Configuration: http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx – Joe Jun 27 '12 at 16:49
  • @Joe Thanks for the remark. I edited the answer to clarify what I meant. Obbiously, protected configuration is OK - that's the reason why it was invented in the first place. – Sergey Kalinichenko Jun 27 '12 at 17:00
1

We certainly can. The issue with this is resource files are compiled into an assembly so you can't override the value without recompile of the project. So I would recommend using configuration files instead of resource files for configuration parameters such as connection string.

amdmax
  • 771
  • 3
  • 14
  • We had a workaround for this. Which injected connection string using IoC into NHibernate configuration method. – amdmax Jun 27 '12 at 15:45
1

Can we place -> yes. It is just a string,

Advantages -> 1. Localization ( Not all databases support globalization for all cultures, but still i have seen username/password in different language) 2. Passwords can be encrypted/obfuscated

Disadvantages 1. Modification requires recompilation. (This is the basic reason why they can be set in config file)

Tilak
  • 30,108
  • 19
  • 83
  • 131
0

You can put any string in a resx file, so yes it's possible to put db connection information there. It's much more customary to put it in the app.config file.

bmm6o
  • 6,187
  • 3
  • 28
  • 55