Sunday, June 19, 2016
So you have to work with multiple connection strings that you want to use in the Web.config file and you want to use it in the code behind file. Below are the steps to retrieve multiple connection strings in the web.config file programmatically.
1. Make sure you have connection strings in your Web.config file. A connection string looks something like this.
2. Now in the Default.aspx.cs file type in the following in the top of the file, where all the using statements are to use the System.Web.Configuration namespace
Obviously you can use it in any .cs file, the Default.aspx.cs is just an example.
3. In the Page_Load method type in the following line
Or you can just do this and you don't have to assign WebConfigurationManager.ConnectionStrings to a variable at all. A lot less typing.
If you run the code you will get the following
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
Data Source=(local);Initial Catalog=Northwind;Integrated Security=True
Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
Notice the attached SQLEXPRESS file aspnetdb.mdf file in your results, well it's because there's a
hierarchy in the Web.Config file and before .NET even look at the current Web.config in your web application it looks in the .NET Framework install folder for the default Web.config file, this file is call the "Root Web" configuration level . That's why the first item in the ConnectionStringSettingsCollection is not Northwind.
If you just want the current web applications Web.config connection strings section all you have to do is add the <clear/> tag right below the <connectionsStrings> opening tag in the current web application's Web.config file, like the xml code below.
The <clear/> tag tells .NET to remove all of the inherited configuration information and just use the configuration of the current web application. So now the result will be
Data Source=(local);Initial Catalog=Northwind;Integrated Security=True
Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
Which is what you really wanted in the first place.
1. Make sure you have connection strings in your Web.config file. A connection string looks something like this.
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="AdventureWorksConnectionString" connectionString="Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
2. Now in the Default.aspx.cs file type in the following in the top of the file, where all the using statements are to use the System.Web.Configuration namespace
using System.Web.Configuration; using System.Configuration;
Obviously you can use it in any .cs file, the Default.aspx.cs is just an example.
3. In the Page_Load method type in the following line
ConnectionStringSettingsCollection connectStrings = WebConfigurationManager.ConnectionStrings; foreach (ConnectionStringSettings cstr in connectStrings) { Response.Write(cstr.ConnectionString + "<br>"); }
Or you can just do this and you don't have to assign WebConfigurationManager.ConnectionStrings to a variable at all. A lot less typing.
foreach (ConnectionStringSettings cstr in WebConfigurationManager.ConnectionStrings) { Response.Write(cstr.ConnectionString + "<br>"); }
If you run the code you will get the following
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
Data Source=(local);Initial Catalog=Northwind;Integrated Security=True
Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
Notice the attached SQLEXPRESS file aspnetdb.mdf file in your results, well it's because there's a
hierarchy in the Web.Config file and before .NET even look at the current Web.config in your web application it looks in the .NET Framework install folder for the default Web.config file, this file is call the "Root Web" configuration level . That's why the first item in the ConnectionStringSettingsCollection is not Northwind.
If you just want the current web applications Web.config connection strings section all you have to do is add the <clear/> tag right below the <connectionsStrings> opening tag in the current web application's Web.config file, like the xml code below.
<connectionStrings> <clear/> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="AdventureWorksConnectionString" connectionString="Data Source=(local); Initial Catalog=AdventureWorks;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
The <clear/> tag tells .NET to remove all of the inherited configuration information and just use the configuration of the current web application. So now the result will be
Data Source=(local);Initial Catalog=Northwind;Integrated Security=True
Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
Which is what you really wanted in the first place.
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Aivivu đại lý vé máy bay, tham khảo
ReplyDeleteVé máy bay đi Mỹ
máy bay từ mỹ về việt nam
mua vé từ nhật về việt nam
ve may bay tư duc ve viet nam
chuyến bay từ canada về việt nam
Các chuyến bay từ Incheon về Hà Nội hôm nay
Chi phi cho chuyen gia nuoc ngoai