Problem with ConnectionString

1

I have a problem with the connection string:

Putting it directly into the SqlConnection works for me:

SqlConnection connection = new SqlConnection("server=DESKTOP-Q5REQCB\MSSQLSERVER01 ; database=Synergy ; user id = Synergy; password = SynergyPass");

Trying to access it from the web.config does not connect:

SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);

web.config:

 <connectionStrings >
    <add
      name="myConnectionString"
      connectionString="server=DESKTOP-Q5REQCB\MSSQLSERVER01 ; database=Synergy ; user id = Synergy; password = SynergyPass "
      providerName="System.Data.SqlClient"/>
    </connectionStrings>

And I try to print by console

Debug.WriteLine(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString)

And print the chain perfectly.

What is the problem?

    
asked by Jmyebenes 15.02.2018 в 13:06
source

1 answer

2

When you add a ConnectionString in the configuration file, it is not necessary to escape the backSlash by putting it double \ , you have to put only one \ . In this case, it should look like this:

<connectionStrings >
    <add
       name="myConnectionString"
       connectionString="server=DESKTOP-Q5REQCB\MSSQLSERVER01 ; database=Synergy ; user id = Synergy; password = SynergyPass "
       providerName="System.Data.SqlClient"/>
</connectionStrings>
    
answered by 15.02.2018 / 13:28
source