質問

I try to connect to my database and I can do that using following code:

using (SqlConnection conn =
    new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=DocumentManager; Persist Security Info=True; Integrated Security=True")) 
{ ... }

But, when I try this:

string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString)) { ... }

it doesn't work anymore. My Connection String looks like this:

<add name="DatabaseConnection" connectionString="Data Source=.\\SQLEXPRESS; Initial Catalog=DocumentManager; Persist Security Info=True; Integrated Security=True" />

And, I can read the connectionString variable and it looks exactly like the string in the first case.

役に立ちましたか?

解決

Your in-code connection string has an escaped "\" in it.

Try changing your web.config to:

<add name="DatabaseConnection" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=DocumentManager; Persist Security Info=True; Integrated Security=True" />

The "\" does not need to be escaped in your web.config.

This is an example of escaping the backslash.

他のヒント

Try this

string connectionString = System.Configuration.ConfigurationManager.AppSettings("DatabaseConnection");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top