Search code examples
c#visual-studio-2019database-connection

Why can't I modify and do anything with my DB tables through vs2019


enter image description here

When I connect my local DB with my project in vs2019 it says that my connection was successful, all good. But when I right click on a table and try to do whatever it pops up an error message with "Failed to retrieve server version".

Another thing that makes a problem is my connection in is:

public static SqlConnection connection = new SqlConnection("Data Source=DESKTOP-0R58GC3**\**SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=True");

it says that this part:

Source=DESKTOP-0R58GC3\SQLEXPRESS

More particularly the \ is unrecognized, this is the error that it gives me after trying to build it:

Severity Code Description Project File Line Suppression State Error CS1009 Unrecognized escape sequence School Database C:\Users\merti\OneDrive\Работен плот\School Project\School Database\School Database\DB.cs 14 N/A

Tried reconnecting and connecting a hundred times, tried to see of all my services are running (they are expect for SQL Agent I don't why but I can not start it).


Solution

  • The backslash \ is a escaping character used to deal with special characters.

    For this reason, if you need to consume it as a regular character, you need to escape it:

    public static SqlConnection connection = new SqlConnection("Data Source=DESKTOP-0R58GC3\\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=True");
    

    Notice how it's duplicated. This is because I am using it to indicate that I am escaping the next character, which is itself.

    You can read more about it here.