.Net core connection string to MSSQL from OSX - sql-server

I am trying to connect to an MSSQL server from a C# ASP.Net core application running in OSX. The MSSQL is on a separate machine which firewall is configured to let connections via TCP to port 1433 and via UDP to port 1434.
The connection string I am using is:
Server=IP\SQLEXPRESS;Database=DB_NAME;user id=****;password=*****
What is very confusing at this point is that the very same connection string works flawlessly from Windows and both Visual Studio and Visual Studio Code. The moment I reboot in my OSX, then VS Code opens the project but raises the dreaded
System.Data.SqlClient.SqlException: 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: 40 - Could not open a connection to SQL Server)
exception. I have no idea what to do, as this code should work. What does the OSX implementation of the SQL Server connector do that is so different?

As Giuseppe said, you need to use IP,port. Named instances can run on various ports and Windows clients (i.e. SQL Management Studio) detects port by asking SQL Browser service.
In you case, you should open SQL Configuration Manager, Go to SQL Server Network Configution -> Protocols for SQLExpress (or whatever is name of the instance) -> TCP/IP (right click) -> Properties -> Ip Adresses tab, scroll to bottom and see (and/or) set the current port.

Apparently the *nix implementation of the UseSQLServer method does not support instance names. Therefore IP,port is the way to go.

Related

How to connect to an SQL Server on Parallels VM

I'm attempting to connect to a test Database on a Parallels VM on Mac using Azure Data Studio, but I'm getting the standard, generic connection error:
System.Data.SqlClient.SqlException (0x80131904): 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: 40 - Could
not open a connection to SQL Server)
Steps I've taken so far to open the connection include:
Opening port 1433 through windows firewall
Ensuring remote connections are allowed on the SQL instance
Setting the TCP/IP Protocol to 5171 for IPAll
Running SQL Server and SQL Browser Instances
Adding a rule to windows firewall for sqlservr.exe to allow all connections
Enabling the SA account and adding a password (password is correct - I've double checked in SSMS on the VM)
I'm able to connect from the windows VM but not the Mac.
My connection string is below (IP is correct, again I've double checked):
Is there a step I've missed or some extra config that I need to do on the VM itself maybe? Or maybe the connection string isn't quite right? I've also tried using the Machine Alias to connect to the instance (SQLEXPRESS) - but still no joy. Anyone know what the problem might be because it's driving me crazy and I can see that other devs have made it work, so I know it's possible. My version of Parallels is Desktop 15, I don't know if that makes a difference or not?
Ok so turns out I needed to open the IPAll port I had set for the TCP/IP Config which in my case was 5171 as well as adding a rule to allow SQL Server Browser to connect to the internet.
For those of whom the above method doesn't work, try Bridged Network instead of Shared Networking in the Network settings and replace the old IP address with the new one.
I am able to connect to Microsoft SQL Server (in Parallels Desktop 16) using Visual Studio Code in macOS Big Sur.

Connecting to SQL Server with .Net code on OS X

I am trying to port a web api that acts as a front-end for an SQL Server database. The web api works fine under Windows and when deployed to IIS, works from .Net Core on Windows, but gives a connection error when ran from within OSX or Linux.
The connection string that worked from within Visual Studio in Windows was
connectionString="data source=A\B;initial catalog=C;
persist security info=True;user id=X; password=Y;
MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient"
which is then translated into the connection string for the .Net core variant as:
Server=A\B;Database=C;user id=X;password=Y;
The project compiles fine, even works under .Net core in Windows, but crashes when run from a *nix machine.
The error is:
Unhandled Exception: System.Data.SqlClient.SqlException:
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: 35 - An internal exception was caught) --->
System.AggregateException: One or more errors occurred. (No such device
or address) --->
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No
such device or address
I can ping the address, but I guess the combination of valid address (A) and server instance (B) does not work with a simple slash under *nix.
Server=A\B;Database=C;user id=X;password=Y;
says that you are accessing to B instance on a Windows machine whose Netbios name is A. Probably the windows machine where it worked was "A" itself.
For a remote connection though there are things that you should have done:
The SQL server instance should be set to allow remote connections.
The port on SQL server listens to must be enabled for access on firewall.
Instead of Netbios name, it is better to use IP address, along with port number if it is not the default port (1433).
You may start trying (where 1.2.3.4 is the windows machine's IP):
1.2.3.4\B;Database=C;uid=X;pwd=Y
For a detailed installation and setup of SQL Express on host PC, enabling for remote access and then accessing from remote computer, you may watch my short video series. Those videos were about installation of my application but most of it contains installing MS SQL server and accessing from remote location. It is a 4 parts series and here is the link to first part:
AccuSQL Installation 1/4

Could not connect to SQL Server instance from one of the machines

I have two machines (a laptop and a desktop) connected to the same network throgh the same router and an SQL Server with 2 instances: ODD and EVEN, both instances have the same configuration (as far as I can see), including sa account settings.
From my desktop I can connect to both instances, but from laptop I can connect only to the EVEN instance, when I try to connect to ODD instance I get the following error:
Microsoft Visual Studio
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
OK
I know that:
I have correct password, because I can connect to EVEN
I have correct firewall settings, because I can connect to EVEN and both instances use the same TCP/IP port 1433
I have properly configures server, because I can connect to the both instances from desktop.
How should I troubleshoot this issue? Is there any more detailed information about what went wrong? Why does Visual Studio uses named pipes and does not try TCP/IP for example?
Check Sql Server Configuration manager on your client and server to see if TCP/IP protocol is enable or not. you can even disable Named Pipes protocol there and see how it will effect on VS error message.
you can even check server setting by trying below command from command shell.
telnet server-ip 1433

Connecting to SQL Server Named Instance from Windows 64bit

I have both java and .net applications running on an app server using Microsoft Windows Server 2003, Enterprise Edition. These are being migrated to another app server Windows 2008 64-bit machine.
All applications connect to the same SQL Server 2005 database, on a named instance.
So far I have tried to move the applications exactly as they are, with no changes in the configuration files, from the old box to the new box.
On the new app server, neither the java nor the .net applications connect to the database (named instance).
JDBC error message: "The connection to the named instance has failed. Error: java.net.SocketTimeoutException: Receive timed out."
The .net error message: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible."
If I change the DB config to one that is not on a named instance it works on the new app server.
The database is setup correctly, because we were able to connect on the old app server. I can ping the database server from the new app server.
Is there any reason this won't work on the new app server?
java connection string: "jdbc:microsoft:sqlserver://[dbservername];SelectMethod=Cursor;instanceName=[dbinstance]"
.net connection string: "Server=[dbservername]\[dbinstance];Database=Risk_DB;Uid=[user];Pwd=[pwd];"
UPDATE
Per suggestions in the answers, I got the instance port number. I also installed SQL Server Management Studio so I can eliminate my apps as the problem points. From Management Studio, if I use [servername][instancename], I get the message "a network-related or instance-specific error while establishing a connection to SQL Server..." But it works when i use [servername],[port]. Not sure if there's anyway to work around this?
UPDATE #2 Escalated the issue to the infrastructure/server/network team. They disabled windows firewall on the new app server. Presto, now I can connect to [dbservername]\[dbinstance] in Management Studio, and all apps are working using existing configuration files.
Your named instance is going to be running on a different port. Port 1433 (the default for a default instance) is probably open, and the port that the named instance is running on is probably blocked. You can check the port in the error log for the named instance (assuming you can connect locally, in Object Explorer, expand the server, expand Management, expand SQL Server Logs, right-click current, and choose "View SQL Server Log" IIRC), it will say something like this on startup:
Server is listening on [ 127.0.0.1 <ipv4> 3587 ].
That last number is the port number that needs to be accessible from your remote machine and whatever network devices and services it has to go through to get there. If you don't find a line like that, it's possible that TCP/IP is not enabled for the named instance. On that server, go to SQL Server Configuration Manager, expand SQL Server Network Configuration, click on "Protocols for " and make sure TCP/IP is enabled in the right pane. If you have to enable this you'll need to restart SQL Server for it to take effect.
If it is already enabled (or once you enable it and restart the service), you should be able to refresh this view and validate the port that is being used if you right-click TCP/IP, hit properties, and move to the IP Addresses tab. You can see the ports currently being for each IP. Here there will be multiple IPn sections and an IPAll section. For each IP, you can change the "TCP Port" box to a port you want to use (and delete any values in all the "Dynamic TCP Ports" box to 0). Hit Apply and restart the service. This will again require a restart of the service but will allow you to specify a specific port so you can add an exclusion to your firewall (or make use of one that already exists, assuming this server isn't already using that port).
Possible issues:
Firewall maybe blocking connections.
The instance name is not the same as specified in the connection string.
The connection string specifies a different port or SQL Server is running on a different port rather than the default 1433

establish remote connection to a SQL server instance

I am not able to establish remote connections to SQL Server 2008 enterprise edition.
I have followed and implemented a lot of tutorials available. These are the things i have done:
enabled TCP/IP connections in SQL configuration manager.
added a rule to allow inbound connections at port 1433.
allowed remote connections in the properties window of the concerned instance.
But it still gives the error
I am trying to install samples for replication using AdventuresWorks database and i am not able to install the sample.
here is the error i got
Attempting to create the 'ReplData'
share that replication uses as the
working directory.
A subdirectory or file C:\Program
Files\Microsoft SQL
Server\100\Samples\Replica
tion\ReplData already exists. The name
has already been shared.
More help is available by typing NET
HELPMSG 2118.
HResult 0x274D, Level 16, State 1 TCP
Provider: No connection could be made
because the target machine actively re
fused it.
Sqlcmd: Error: Microsoft SQL Server
Native Client 10.0 : A network-related
or in stance-specific error has
occurred while establishing a
connection to SQL Server . Server is
not found or not accessible. Check if
instance name is correct and i f SQL
Server is configured to allow remote
connections. For more information see
SQL Server Books Online.. Sqlcmd:
Error: Microsoft SQL Server Native
Client 10.0 : Login timeout expired.
The publication was created
successfully. You must create a
virtual directory to use Web
synchronization. If you plan to use a
separate Web server, you must run the
Web Synchronization Wizard on that Web
server. (A certificate that supports
SSL must already be installed.)
Do you want to create a virtual
directory named SalesOrders for Web
synchronization on S2NWORKSTATION?
[y,n]
Start the SQL Management Studio
Connect to the server.
Then choose the server in the Object-Explorer and right-click on properties.
Then check the "connections" settings, there you can/have to allow remote connections for this database server.
Are you connecting to a default instance or named instance? If a named instance, then you you should have the sql browser windows service running. Also ensure that remote connections are allowed (I believe this is a different setting than simply turning on TCP/IP).
One tool that could be helpful is PorQryUI (Port Query), it will test to see if the proper ports are open.
I too, had this problem. I solved by doing a couple of things:
1. Make sure the SQL Browser service is started on my local machine
2. Make sure UDP Port 1434 in the firewall is open for outgoing on the local machine
3. Make sure UDP Port 1434 is open in the firewall for incoming on the remote machine.
The UDP Ports are for the SQL Server Browser. Apparently, Visual Studio uses that to detect that the remote server exists.
This Stack Exchange thread was very helpful to me.

Resources