Connection String Error in Visual Studio 2015

Asked

Viewed 190 times

2

When I place the connection string, the following error appears:

Con = new SqlConnection("Data Source=(localdb)\MSSQLLocalDB;OutrosParametros");

Represents text as a series of Unicode characters

When I put two bars (\\) the error adds up.

I can use \\?

2 answers

3


The message

Represents text as a series of Unicode characters

is not a mistake, is the description of string.

The code actually has an error which is to use the \ as literal, this character needs to be escaped.

There are two options for this:

  1. Use \ to escape that character only.

    Ex.: "Teste\\Teste" amounts to Teste\Teste.

    new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;OutrosParametros");
    
  2. Use @ at the beginning of string. This will cause the text to be represented that way. In other words, the string turned into a string literal (or Verbatim string).

    Ex.: @"Teste\nTeste" amounts to Teste\nTeste.

    new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;OutrosParametros");
    

1

Put @ before your quotes ("")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.