472,102 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

Write the Connection String in app.config and Read it in the Code

10
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
May 23 '07 #1
3 39133
yahya30
10
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
May 23 '07 #2
yahya30
10
I should have said that this connection string is not a standard one because it might differ if the Sql Server is set to be started and run on a specific account rather than system account. But if you set the Sql Server to start and run on System Account then I think the connection string will be the same as the one written herebefore.
To check this setting, just open the Sql Server Enterprise Manager-> right click on your Sql Server in the tree view, select Properties -> Select Security Tab -> and check for the Startup service account.

Kind regards,
Yahya
May 23 '07 #3
Awesome, i spent three hours looking for something that actually works. Thanks
Mar 18 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Shaun Ram | last post: by
6 posts views Thread by Jon Davis | last post: by
12 posts views Thread by Charlie | last post: by
2 posts views Thread by Alan T | last post: by
9 posts views Thread by =?Utf-8?B?Vmlua2k=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.