Question

I have done till this and i don't know how to call the connection string into my console application from app.config

<add name="VirgoPlaza" 
     connectionString="Data Source=JEAN-DANIEL\SQLEXPRESS;Initial Catalog=VirgoPlaza;Integrated Security=True"
     providerName="System.Data.SqlClient" />

Can someone help please?

Was it helpful?

Solution

First add reference to System.Configuration. In Solution explorer, right click on References, Add References.

Add reference to System.Configuration Manager.

enter image description here

Then access the connection string like:

var connectionStrings = System.Configuration
                            .ConfigurationManager.ConnectionStrings;
var yourConnectionString = connectionStrings["VirgoPlaza"];

Assuming you have stored connection string in App.Config like:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="VirgoPlaza" connectionString="Data Source=JEAN-DANIEL\SQLEXPRESS;Initial Catalog=VirgoPlaza;Integrated Security=True"
            providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

VB.Net equivalent would be:

Dim Cons As String = System.Configuration
                        .ConfigurationManager
                        .ConnectionStrings("VirgoPlaz").ConnectionString

You may see: Connection Strings and Configuration Files

OTHER TIPS

I suppose that you have your connection string defined inside this block of you app.config

<connectionStrings>
    <add name="VirgoPlaza" 
         connectionString="Data Source=JEAN-DANIEL\SQLEXPRESS;Initial Catalog=VirgoPlaza;Integrated Security=True"
         providerName="System.Data.SqlClient"/> 
</connectionStrings>

If this is the case then you retrieve the string from the app.config using

Dim cnString = ConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString

A quick cheat is use your server explorer to connect, and then copy the connection string from there. Alternatively just use the new entity framework and you can do it via a wizard as well.

EDIT: Ok, it seems you have the connection string now - however does this mean that you cant connect, could never connect or does it mean you are just stuck making it work in general?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top