-1

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:
enter image description here

And the properties: enter image description here

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 enter image description here

Also
enter image description here

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.

ehtio
  • 179
  • 3
  • 11
  • 2
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/249342/discussion-on-question-by-carlosmira-connecting-to-a-remote-sql-server-sqlexpre). – Samuel Liew Nov 05 '22 at 07:45

1 Answers1

0

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

  • 1
    I would have prefered to continue with the way I was doing things, but this seems like a good alternative. Thanks for the links. – ehtio Nov 21 '22 at 08:38