Introduction
In this article we will learn How to write connectionstring in C#. ConnectionString in Asp.Net. Why do we need ConnectionString in C#. Write connection string in Web.Config file. Read connectionstring from Web.config.
Previous Updates
In previous articles we have learnt EncryptByPassPhrase and DecryptByPassphrase function in Sql Base64 Encoding And Decoding In SQL Server. Transform multiple rows into one comma separated string column. What is Cursor in sql and use of cursor. AngularJS basics.
Description
You can write your connectionstring in Web.Config file or also in every page where you need the database connection. But by writing this on web.config file you don't need to write this in every page. You just reference the ConnectionString name in every page where you need it.
ConnectionString In Web.Config
In this Connection String -
Data-Source - Sql Server Instance Name.
Initial Catalog - Your Database Name
Integrated Security - By default its false. If we did not use the Sql Server authentication (UserName and Password) then we give this true.
ConnectionString In Web.Config
<connectionStrings>
<add name="ConStr" connectionString="Data Source=guruji-PC;Initial Catalog=EmpDetail;Integrated Security=True"/>
</connectionStrings>
|
In this Connection String -
Data-Source - Sql Server Instance Name.
Initial Catalog - Your Database Name
Integrated Security - By default its false. If we did not use the Sql Server authentication (UserName and Password) then we give this true.
For use the ConnectionString in Asp.Net Web Application C# code page we need to reference the System.Configuration Assembly in or application.
Add System.Configuration Assembly in Project
1. Go to Solution Explorer and Right click on your Project. Select Add and click on Add reference
Note- you will find this step in VS13 nd 15. If you are using VisualStudio 2010 or 12 then by right clicking you can directly seen Add reference option.
2. A new Popup window will open and search for System.Configuration and add this assembly by clicking on checkbox just front of its name. And click on OK.
You successfully Add a System.Configuration assembly reference.
Read the ConnectionString From Web.Config
On your .Cs page file you can use the web.config global connection string by using the below code
Add System.Configuration Assembly in Project
1. Go to Solution Explorer and Right click on your Project. Select Add and click on Add reference
2. A new Popup window will open and search for System.Configuration and add this assembly by clicking on checkbox just front of its name. And click on OK.
You successfully Add a System.Configuration assembly reference.
Read the ConnectionString From Web.Config
On your .Cs page file you can use the web.config global connection string by using the below code
string connectionString =
ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
|
0 comments:
Post a Comment