简体   繁体   中英

connection string from web.config fails where identical hard-coded string succeeds (ASP.NET with C#)

I'm getting some strange behaviour with a connection string passed in from a config file.

I am building an n-tier website (with C# class libraries and a C# ASP.NET website), and got a basic page working what pulls some data from the database (Microsoft SQL Server 2008), through the data access and business tiers and displays it on a web page. This is successful with a hard-coded connection string in the data access tier.

To improve the architecture, I am removing the connection string to the web.config file in the website. The connection string is successfully retrieved from web.config, but the connection fails when the .open() method is called with this connection string.

With this constructor, the database connection works perfectly:

public PathwayAccess() {
    connectionString = "server=.\\MYSERVER;database=MyDatabase;Integrated Security=SSPI;";
}

With the following, the string is retrieved, can be written out to debug and looks fine, and throws no exceptions:

EDIT web.config

<?xml version="1.0"?>
<configuration>

    <system.web>
      <compilation debug="false" targetFramework="4.0" />
      <customErrors mode="Off"/>
    </system.web>

  <connectionStrings>
    <clear/>
    <add name="MySqlClientConnectionString"
     providerName="System.Data.SqlClient"
     connectionString="server=.\\MYSERVER;database=MyDatabase;Integrated Security=SSPI;" />
    <!-- EDIT: problem was the escaped \ in the connection string:
               should have been .\MYSERVER instead of .\\MYSERVER as
               it doesn't need to be escaped in the config file -->
  </connectionStrings>

</configuration>

and new constructor for my data access class:

public PathwayAccess() {

    ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
    if (settings != null) {
        foreach (ConnectionStringSettings cs in settings) {
            if (cs.Name == "MySqlClientConnectionString" && cs.ProviderName == "System.Data.SqlClient") {

                connectionString = cs.ConnectionString.ToString();
                return;
            }
        }
        throw new Exception("The SqlClient connection string named 'MySqlClientConnectionString' was not found in connection.config. "
            + "Found " + settings.Count + " connection strings");
    } else {
        throw new Exception("Could not retrieve the connection string from a configuration file. Check the connection strings section of the relevant config file");
    }
}

But the following code will work after the first constructor above, but throws an InvalidOperationException after the second (with message "Instance failure"):

myConnection = new SqlConnection(connectionString);
myConnection.Open();

I'm very puzzled by this - for some reason the connection string from the config file looks identical to the hard-coded one, but behaves differently. Any idea what might be going on?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM