In this article I will explain how to read or write connection strings in web.config file using asp.net.
In Previous article I explained clearly about Encrypt or Decrypt connection strings in web.config file using asp.net. Now I will explain how to write connection strings in web.config file and how to read connection strings from web.config file using asp.net.
<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient"/>
</connectionStrings >
|
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.Data.SqlClient"/>
</connectionStrings >
|
using System.Configuration;
|
using System;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get connection string from web.config file
string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
//create new sqlconnection and connection to database by using connection string from web.config file
SqlConnection con = new SqlConnection(strcon);
con.Open();
}
}
|
Imports System.Data.SqlClient
Imports System.Configuration
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Get connection string from web.config file
Dim strcon As String = ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString
'create new sqlconnection and connection to database by using connection string from web.config file
Dim con As New SqlConnection(strcon)
con.Open()
End Sub
End Class
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
13 comments :
Nice Blog and I am going to congratulate you to get higher traffic within your blog.There are a lot of information within your blog which is really helpful for worldwide developers and they can learn and also get a solution for their technical problem .
It is really good that you are giving extra time to maintaining the blog and give revert back answer to all world wide developer.
Best of luck for your upcoming career.
http://www.codeprojectdownload.com
how can I connect multi Dababase server using this connection string?
hello
i have MS access 2007 connection string
I use asp.net 2010 express adition
I right below code in my website
IN Recent_artical.aspx.cs file
String strCon = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
OleDbConnection con = new OleDbConnection(strCon);
con.Open();
which code right in Web.config file
Please urgent answer in my gmail id
hetalmodi43@gmail.com
hi suresh its been very interesting by reading ur articles....
can u plz give me the clear difference of app settings and connection strings which one is better to use?
r they both same if not how they differ?
can u plz explain on this
SPECIAL REQUEST CAN YOU GUIDE ME HOW TO CONNECT THE DATABASE IN ASP.NET WITH ORACLE
fhghjgjhgjhjhoikjahgdfsdgfbbbbb
very helpful code
thank you
pls tell if i want to use multiple connection string one for access and one for sql ..how can i achieve this
Nice article, your article are very help full........Thanks to you from bottom of my heart
hi sir iam veerendra u r regular follower sir pls
help me i want remote database connection through
C# windows Applications
Very Nice article Sir.I really wants to thank you for such a nice explanation.