Asad Iftikhar
Asad Iftikhar

Reputation: 717

SQL Server connection string error cannot connect

This is my connection string

I have added double \\ for the unrecognized escape sequence in vs 2019

SqlConnection db = new SqlConnection("Server=localhost\\DESKTOP-8QL52AL\\ASADI; Database=atm;  Integrated Security=True;");
db.Open();

Error at line -26 check your server name or check your SQL Server is configured for remote connections

SQL SERVER CONNECTION IMAGE

Upvotes: 0

Views: 5432

Answers (3)

barath
barath

Reputation: 1

Basic Sql Server connection string syntax:

connetionString="Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

If you have a named instance of SQL Server, you'll need to add that as well.

"Server=localhost\sqlexpress"

Upvotes: 0

marc_s
marc_s

Reputation: 754488

You server/instance name is all wrong:

"Server=localhost\\DESKTOP-8QL52AL\\ASADI;

You can either have

  1. just the server name (or IP address) when connecting to the default, unnamed instance on your machine

     Server=DESKTOP-8QL52AL
    
  2. or you can have the server name and an instance name, if you're connecting to a named instance

     Server=DESKTOP-8QL52AL\\ASADI
    

And in both cases, you can replace the actual physical machine name with localhost to connect to the local machine (without specifying its explicit name):

    Server=localhost
    Server=localhost\\ASADI

but you CANNOT have localhost AND your explicit machine name DESKTOP-8QL52AL in a single connection string - that just doesn't make any sense and isn't supported.

Upvotes: 2

VertexGames
VertexGames

Reputation: 97

If the SQL server is on the local machine and you only have 1 sql instance installed

"Server=localhost; Database=atm; Integrated Security=True;"

if its SQL EXPRESS use

"Server=localhost\SQLEXPRESS; Database=atm; Integrated Security=True;"

Upvotes: 0

Related Questions