Hi all,
I want to know how to write the connection string in app.config and read it in the code, note that I'm using Vb.net desktop application.
Kind regards,
Yahya
I have found it, thanks...
The following code must be written in app.config in order to save the connection string in a common place outside the code:-
<add key="Conn" value="workstation id=YourServerName;packet size=4096;integrated security=SSPI;data source=YourSqlServerName;persist security info=False;initial catalog=YourDataBaseName" />
Note that istead of YourServerName written above you must write down your server name (The machine that is holding the Sql Server being used for your application), this server might be your machine.
istead of YourSqlServerName you must write down your Sql Server name as defined on the server/your machine.
istead of YourDataBaseName you must write down the name of your database as defined in the Sql Server.
Then to read the connection string in the vb.net code I wrote:-
Private sConn As String
Private Conn As System.Data.SqlClient.SqlConnection
sConn = System.Configuration.ConfigurationSettings.AppSett ings("Conn")
Conn = New System.Data.SqlClient.SqlConnection(sConn)
Note that I got the value available in app.config by using System.Configuration.ConfigurationSettings.AppSett ings("Conn"), note that I gave a key called "Conn" for this value in app.config, and put that key between brackets in System.Configuration.ConfigurationSettings.AppSett ings("Conn").
You can name this key in app.config with any name you want but you have to call it with the same name in the code.
I mean that if you name it as "MyConn" then you have to call it like this in the code:-
sConn = System.Configuration.ConfigurationSettings.AppSett ings("MyConn")
Conn = New System.Data.SqlClient.SqlConnection(sConn)
So you will not be able to call it as:-
sConn = System.Configuration.ConfigurationSettings.AppSett ings("Conn")
Conn = New System.Data.SqlClient.SqlConnection(sConn)
If you have any other suggestions please let me know.
Kind regards,
Yahya