Problem connecting to a database via app.config

Expensive

New member
Joined
Apr 12, 2020
Messages
2
Programming Experience
Beginner
I'm new to C # development, I can't connect to my database via app.config, I don't know how to code the connetion in Form1_Load, here is my app.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!-- Les paramètres de l'application utilisateur et de propriété configurée se placent ici.-->
<!-- Exemple : <add key="settingName" value="settingValue"/> -->
<add key="BaseType" value="MSSQL" />
<add key="Serer" value="192.168.16.100\SQL2014" />
<add key="Base" value="REF" />
<add key="BaseDef" value="TESTREF" />
<add key="DossierSav" value="c:\bb\" />
<add key="ImressionDirecte.Checked" value="false" />
<add key="Soc.Text" value="XXXX" />
<add key="Licence" value="XXXX" />
<add key="Multi" value="" />
<add key="Licence" value=XXXX" />

</appSettings>

</configuration>

I await your help to advance in my project and thank you in advance.
 
The code to connect to your database will be exactly the same as it always is. You need to construct a connection string from the appropriate values in your config file. The actual question you need to answer is how to read specific values from the appSettings section of a config file and that is something that you can search for on the web. This problem has got nothing at all to do with database connections. It doesn't matter what you're going to do with the data after reading it. Reading it from the config file will still be the same. If you work a bit harder at identifying the actual problem then finding a solution is much easier because you can use the appropriate search terms.

Of course, if you're talking about connection strings then it's more appropriate to store a single value in the connectionStrings section rather than multiple values in appSettings. That section is, after all, specifically intended for connection strings.
 
+1 regarding lumping all the pieces that you need to make a connection into a single connection string.

If you are worried about parsing out the parts of the connection string, the DbConnectionStringBuilder is not only great for building a connection string, but it is also good for parsing as well.
 
Back
Top Bottom