✅ [ASP NET Core] How do i connect mssql db to vs code project?

I've got some problems with connecting db when i switched to vs code. Now when I try to make "DefaultConenction": "my connection string" it complains about \ in string.
20 Replies
Shinigami
Shinigami2y ago
I think connection string syntax is wrong
LiteFlow10)
LiteFlow10)OP2y ago
i copied this from visual studio and also i checked previous projects they also had \ in syntax
Shinigami
Shinigami2y ago
Can you paste the full string?
LiteFlow10)
LiteFlow10)OP2y ago
Data Source=DESKTOP-7IMIBG3\SQLEXPRESS;Initial Catalog=GiveawayWebAppDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False
Shinigami
Shinigami2y ago
Remove the space from initial catalog
LiteFlow10)
LiteFlow10)OP2y ago
it doesnt work. Also it doesnt work if i remove all spaces
Shinigami
Shinigami2y ago
Don't think that's the issue
LiteFlow10)
LiteFlow10)OP2y ago
but it does if i remove \
Shinigami
Shinigami2y ago
\ this is the issue escape char
LiteFlow10)
LiteFlow10)OP2y ago
ye. But how do i connect db if \ is part of connection string?
Shinigami
Shinigami2y ago
Replace \ with \ \ Double \\ Or you could use string literal @" conn string = xyz\db;..." Keep the connection string in a string literal
LiteFlow10)
LiteFlow10)OP2y ago
I did double \ and it worked
Shinigami
Shinigami2y ago
Great
LiteFlow10)
LiteFlow10)OP2y ago
Thank you
Florian Voß
Florian Voß2y ago
@женя. to explain what happened here. Characters that have a special meaning in strings, such as \ which we use for example for new line character \n and other similar characters need to get "escaped" so that compiler taked the backslash as a litteral character rather than trying to apply a special meaning to it as explained by Shinigami, \ is the escape character, so double \\ works cuz first one escapes second one. If you put @ in front that escapes the whole string, thats what peole usually do so that you dont need to put backslashes to escape everywhere
LiteFlow10)
LiteFlow10)OP2y ago
oh good to know tyty
Florian Voß
Florian Voß2y ago
Ok also mention, escaping characters is a very general concept that will occur everywhere in IT field, so keep that in mind. This is not something thats bound to c# and even \ being the escape character is kind of standard across different platforms and stuff so keep this in mind, you learned something very important
LiteFlow10)
LiteFlow10)OP2y ago
appriciate it ty
Florian Voß
Florian Voß2y ago
np you might wanna /close the thread @женя. or is there anything left you wanna ask?
LiteFlow10)
LiteFlow10)OP2y ago
oh right. I was thinking how does it work thanks again
Want results from more Discord servers?
Add your server