简体   繁体   中英

Connecting to a remote SQL Server (SQLExpress) gives "network-related" error, but works using Server Explorer in Visual Studio

I am using.NET 6.0 and I cannot make it work.

I am trying to connect using:

var builder = new System.Data.SqlClient.SqlConnectionStringBuilder
{
    DataSource = $"{ip}\\SQLEXPRESS, 1433",
    InitialCatalog = "xxx",
    UserID = "user",
    Password = "pass"
};
SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = builder.ConnectionString;
thisConnection.Open();

However, I keep getting this

A.network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

Using exactly the same code on Netframework 4.8 works. Anybody care to explain why? Using the same machine as above, if I use the Server Explorer in Visual Studio then the connection is successful. Also works using Python. I tried ussing the connection string from the Server Explorer connection string and still doesn't work. The connection string from that works using the Server Explorer:

Data Source=xxx.xxx.xx.xx\sqlexpress;Initial Catalog=database;Persist Security Info=True;User ID=sysdba;Password=****

Here is the Server Explored connected:
在此处输入图像描述

And the properties: 在此处输入图像描述

And then:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source = xxx.xxx.xx.xx\sqlexpress; Initial Catalog = database; Persist Security Info = True; User ID = sysdba; Password = ******";
conn.Open();

But still fails to connect.

I also set the port to 1433 like suggested in Cannot Connect to Server - A.network-related or instance-specific error and restarted the service在此处输入图像描述

Also
在此处输入图像描述

And still fails to connect using the SqlConnection() (still works using the Server Explorer).

What am I doing wrong? The SQL Server uses SQL Server Authentication, no Windows Credentials. I connect using IP\sqlexpress. It doesn't seem to be a.network related issue, like I saw in other answered questions, since it is working using the Server Explorer in Visual Studio and also in Python.

I was facing the same issue (Net 5.0). You can use the Connnected Services, which is a collection of tools in Visual Studio that will help you connect to the database. I believe this may be a different approach, but it should work since visual studio will be creating the connection string (same was as per Server Explorer).

https://learn.microsoft.com/en-us/visualstudio/data-tools/add-new-connections?view=vs-2022 https://learn.microsoft.com/en-us/visualstudio/azure/overview-connected-services?view=vs-2022

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