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 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
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.