Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
i was developed a project, i wants to find connection string of server at dynamically. and thats must be on app.config, because of the each and every connection from app.config..

so i need to find connectionstring on app.config at dynamically,
that is possible..



Regards
AR
Posted

1 solution

Hi Rajan,

First of all you should set the connection string in the App.Config file. For example I set the connection string for my database as you see here:
XML
<configuration>
  <connectionstrings>
    <add name="TestConnectionstring"
    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </add></connectionstrings>
</configuration> 

After that you use the connection string in your forms using this code:

In your forms you set references that you want to use:
C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

Then you can get the Connection String from the App.Config by using the ConnectionStrings property.
C#
var connectionString=ConfigurationManager.ConnectionStrings["TestConnectionstring"].ConnectionString;

You can use this method in both Windows Forms and ASP.NET projects.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
An@nd Rajan10 28-Dec-13 5:36am    
i can't understand, and actually i am using in server authentication..
Radhakrishnan V 20-Jul-14 6:43am    
nice beginners article anand
Vijay Kant Sharma 22-Jul-14 8:57am    
How i change this "TestConnectionstring" Dynamically or You can say How user is able to change this string.....?

♥…ЯҠ…♥ 1-Sep-14 9:22am    
You may change the connection string value in config file itself.....
Bh@gyesh 2-Sep-14 1:06am    
You can define no of connection string in app.config and can use any of one from code behind.
ie. in class you can define condition and based on condition the connectionstring can be configured.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900