3

This is my first "code first approach" with SQL Server 2012 Express.

I'm trying to create a database but there is a problem with the connection string.

This is my connection string:

Data Source=AMBROGIO\SQLEXPRESS;Initial Catalog=TAP2014_15Project;Integrated Security=True;

This is the connection string which arrives to my setup function:

Data Source=AMBROGIO\\SQLEXPRESS;Initial Catalog=TAP2014_15Project;Integrated Security=True;

With a double \\ after the name of my Pc.

For a test I've tried to write something else as connection string and under debugging it arrives without modification (obviously it raises an error).

Someone can please help me ?

Thanks.

Filippo


Hi, sorry if I reply only now. Yes I was using the debugger to look the string.

Now the error that I have is an exception raise by my method:

 db.Database.CreateIfNotExists();

error:

Exception:Thrown: "Cannot open database "TAP2014_15Project" requested by the            
login. The login failed.
Login failed for user 'AMBROGIO\Filippo'."       
(System.Data.SqlClient.SqlException)
A System.Data.SqlClient.SqlException was thrown: "Cannot open database  
"TAP2014_15Project" requested by the login. The login failed.
Login failed for user 'AMBROGIO\Filippo'."
2
  • 1
    What do you mean by "which arrives to my setup function"? How are you determining the string? My guess is that you're looking under the debugger, which will automatically (and often unhelpfully) escape backslashes... Commented Aug 7, 2015 at 15:57
  • You should add what your problem is. Do you get an error message? What is the code that causes the error message? What is the error message? Commented Aug 7, 2015 at 18:14

2 Answers 2

2

The following line should be in your config file:

<add name="ConnectionStringName"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=AMBROGIO\SQLEXPRESS;Initial Catalog=TAP2014_15Project;Integrated Security=True" />

If you set the connection string from C# code then:

connectionString = @"Data Source=AMBROGIO\SQLEXPRESS;Initial Catalog=TAP2014_15Project;Integrated Security=True";

Example source.

UPDATE

As for the question about login error - there is the same issue aleady resolved. Check it.

Sign up to request clarification or add additional context in comments.

1 Comment

The '@' symbol is critical there, to magically escape the slashes for you. Without the @, you need to add escape slashes on your own. This style of defining string literals is very handy for defining filesystem paths, BTW, per Peter Gluck's answer.
1

The double backslash is an escape seqeuence that resolves to a single backslash when included in a double-quote-delimited string. The backslash is used in many contexts to denote the beginning of an multi-character escape sequence to represent special or non-printable characters. Unfortunately, Microsoft also chose the backslash as the path delimiter in their early DOS operating systems. So to express a path delimiter within double-quote-delimited string you need to use a double backslash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.