0

I want to remove connectionString from App.config and add it to code. I'm using WinForms(C#) and .Net Framework 4.8. I have textBoxes and a datagridview on my form. I'm using Microsoft Access Database (.mdb). I want to do this because database's password is visible in App.config. Is there any way to do that

Here is my app.config XML code :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Locker.Properties.Settings.pw_dbConnectionString"
            connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\files\pw_db.mdb;Persist Security Info=True;Jet OLEDB:Database Password=XYZ"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
</configuration>
Gustav
  • 53,498
  • 7
  • 29
  • 55
  • 3
    It's not a good idea to keep a connection string in code. let us know why do you want to mode it to code? – Vivek Nuna Jun 27 '20 at 13:45
  • So that another user can not get the database's password and open my .mdb file. I'm making a password manager. –  Jun 27 '20 at 13:46
  • Ask the user for the password at runtime when they want to open the DB, not save it anywhere. Then you can build the connection string on the fly. – Alejandro Jun 27 '20 at 14:26
  • Password-protected MS Access files as trivial to unlock. – Joel Coehoorn Jun 28 '20 at 17:04

1 Answers1

0

I would suggest you not to define the connection string in code, better you can encrypt it in the app.config file.

Please follow the following steps:

  • Rename app.config to web.config
  • Open command prompt and type: %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" path of the web.config>
  • Rename web.config back to app.config

If you open the file in notepad, you will see the encrypted value and you can continue using it as you were using before.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • I had encrypted app.config, but I was searching its alternative also. –  Jun 27 '20 at 14:29
  • What alternative?. If you strict at defining it in code, then you can simply use string variable and assign it, but again I'm not suggesting it – Vivek Nuna Jun 27 '20 at 14:35