简体   繁体   中英

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 服务器连接图像

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.

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;"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM