-2

I can connect to my on prem SQL Server by explicitly by passing in the connection string value:

static string connection = @"Server = MYSQL, 455; Database = SomeDatabase; Trusted_Connection = True;";

private static SqlConnection Connection = new SqlConnection(connection);

I don't even know why but you need the @ sign beforehand, how would you include the @ sign in a json file ?

I only know how to put this in json not sure how to put @ in json file?:

}
"database": "Server = MYSQL, 455; Database = SomeDatabase; Trusted_Connection = True;"
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MrRabbit
  • 21
  • 7
  • which JSON file? are you using .net or .net core? – Vivek Nuna May 14 '21 at 09:17
  • @"" allows you to write \ in your string without escaping it. Your connection string above will work fine with or without @. Is that your only question? – ProgrammingLlama May 14 '21 at 09:25
  • I am just wondering if I can put the @"string" in a json file? If so how. So in the connection I just call the @string if that make sense? – MrRabbit May 14 '21 at 09:27
  • I am using .net core – MrRabbit May 14 '21 at 09:27
  • Is there a reason why you would want to do this? And I'm not aware of it being possible. In the JSON you'll just have to escape any \ and " in your string, I believe. – ProgrammingLlama May 14 '21 at 09:30
  • 2
    `you need the @ sign beforehand` no you don't. This has nothing to do with SQL Server, that's the [verbatim string syntax](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#regular-and-verbatim-string-literals) meant to make escaping easier. Nothing in your connection string needs escaping. In any case, the connection string contains the stuff *inside* the double quotes, not the prefix – Panagiotis Kanavos May 14 '21 at 09:37
  • 1
    What is your *actual* problem? Why do you think you need `@`? Did you get an error when you tried to use a regular connection string? What did that string really contain? Why are you using the port 455? That's one of the SMB ports, used by Windows Networking, specifically file sharing. It's *guaranteed* to be in use on any Windows machine. Trying to use it will only lead to errors – Panagiotis Kanavos May 14 '21 at 09:39
  • Don't read to much into ports wasn't going to add a real one, all you need to know if i don't use appsettings.json it works as in code above, but if i use json it doesn't – MrRabbit May 14 '21 at 09:49

1 Answers1

2

I don't even know why but you need the @ sign beforehand...

The @ symbol in C# is called the Verbatim string operator. All it does is say to the compiler 'hey compiler, this string should be treated EXACTLY as it's written, no fancy stuff like turning \n into a new line!'

It allows you to write the string: var path = "C:\\Users\\Deku\\Desktop; as var path = @"C:\Users\Deku\Desktop;, increasing readability and preventing escape sequence errors like accidentally adding a new line.

how would you include the @ sign in a json file ?

For connection strings in your appsettings.json you would use traditional json format, just as you have listed. If any characters need to be prevented from escaping the string, such as the character \ you can simply put a \ before it to tell the string 'hey i want this character displayed as i typed it, do not turn it into something else.

However, with connection strings, yo won't normally need to do that since most connection strings are formatted without using escape sequences too often.

For example here's an appsettings.json showing my connection strings, don't worry the passwords aren't for production.

"ConnectionStrings": {
    "DefaultConnection": "Data Source=mssql-server,1433;Initial Catalog=DingoAspnet;User ID=sa;Password=DingoChat5%;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
    "DingoMessagesConnection": "Data Source=mssql-server,1433;Initial Catalog=DingoMessages;User ID=sa;Password=DingoChat5%;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
    "DingoUsersConnection": "Data Source=mssql-server,1433;Initial Catalog=DingoUsers;User ID=sa;Password=DingoChat5%;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  },

Also make sure your curly braces ({}) are facing the right way.

You have

>    }
>        "database": "Server = MYSQL, 455; Database = SomeDatabase; Trusted_Connection = True;"
>    }

The first curly brace should be facing to the right. Marked here:

>    { <----- this one
>        "database": "Server = MYSQL, 455; Database = SomeDatabase; Trusted_Connection = True;"
>    }
DekuDesu
  • 2,224
  • 1
  • 5
  • 19