How to store the connection string in web.config file

Posted by Akiii under C# on 6/4/2011 | Points: 10 | Views : 3714 | Status : [Member] | Replies : 8
Hi,
Please tell me how to store the connection in web.config file and how to use that in a class or page ?

Also, which is safest and best way to store connection string ?

Thanks and Regards
Akiii




Responses

Posted by: A4u6178 on: 6/4/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

webconfig file looks like.

<?xml version="1.0"?>

<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="TestConnectionString1" connectionString="Data Source=server name;Initial Catalog=DatabaseName;User ID=Username;Password=Password " providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>



aspx.cs page code...
include this namespace using System.Configuration;
string _TestConnectionString1 = ConfigurationManager.ConnectionStrings["TestConnectionString1"].ConnectionString;



Thanks & Regards;
aaa

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/4/2011 [Member] Bronze | Points: 25

Up
0
Down
hi...
thank you ur reply.......
is this is the most secure method ?

thanks and regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/4/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Akiiiiiiiii

You can encrypt the sensitive details such as passwrd, database name etc using encryption methods...........

Regards,
Susan

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/4/2011 [Member] Bronze | Points: 25

Up
0
Down
hi Susan....

Can you suggest some methods for the encryption and decryption....?

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/6/2011 [Member] Starter | Points: 25

Up
0
Down
some one has posted below answer try it.


<connectionStrings>
<add name="MyConnection"
connectionString="Initial Catalog=aspnetdb;
data source=localhost;Integrated Security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>

You can encrypt using visual studio command :
aspnet_regiis -pe "connectionStrings" -app "/TestApp"
where TestApp is virtual directory name is IIS and connectionStrings is config section to encrypt.

If you want to decrypt then use below command :
aspnet_regiis -pd "connectionStrings" -app "/TestApp"



Thanks&Regards
LakshmiNarayana Nalluri.

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/6/2011 [Member] Bronze | Points: 25

Up
0
Down
hi LakshmiNarayana....

Thanks for ur post.....

Its working fine....

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/6/2011 [Member] Starter | Points: 25

Up
0
Down
Dear Lakn2,
Anybody can run the command to decrypt the connection right?

Thanks,
Debata



Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/6/2011 [Member] Bronze | Points: 25

Up
0
Down
hi ndebata.....

I understand ur question......then how do we make it full proof ?

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response