Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to database using localhost\sqlserver

I know we should be aware of such phrases but all of a sudden all my connections to localhost\sqlexpress stopped working. As .\sqlexpress and (local)\sqlexpress work, this wouldn't be a big deal if it weren't for the fact that I am working on a team project where all the connection strings are defined in the former format. And obviously I have a hard time to accept it shouldn't work anymore so I spent several hours analysing and reinstalling several SQL Server versions to no avail. (And on the way breaking the db projects in VS2010 so I needed to reinstall as well...).

But here are the details. As already mentioned I couldn't connect to localhost\sqlexpress but to .\sqlexpress. The same symptoms using SQL Server Manager, and connection strings for deployment and running wcf services. Funnily enough resolving the default instance as localhost works.

First things first, yes the SQL Server is running and just to be sure I enabled TCP/IP and set the port fixed to 1433 although it works on my colleagues machine with TCP/IP disabled. (Somewhere along the way I read that localhost can be resolved using shared memory when using the native SQL client so I guess that is what's happening on his system.)

The setup is only one SQL Server Express 2008 R2 instance and VS2010 on Windows 7 Professional.

Starting up Sql express the errorlog says:

Server is listening on [ 'any' <ipv6> 1433].
Server is listening on [ 'any' <ipv4> 1433].

Netstat says (excerpt):

TCP    0.0.0.0:1433           Machinenename:0             LISTENING
TCP    [::]:1433              Machinenename:0             LISTENING

I note the absence of 127.0.0.1:1433 and any UDP on port 1433

In order to diagnose the problem I use sqlcmd to find out what transport is used:

1. localhost\sqlexpress

sqlcmd -Slocalhost\sqlexpress  -> Shared memory
sqlcmd -Stcp:localhost\sqlexpress ->  SQL Server Network Interfaces: Error Locating Server/Instance Specified

Cannot connect to localhost\sqlexpress from SQL Server Management Studio even if I specify shared memory as protocol

2. localhost

sqlcmd -Slocalhost -> TCP
sqlcmd -Stcp:localhost -> TCP

Can connect to localhost from SQL Server Management Studio

3. (local)\sqlexpress

sqlcmd -S(local)\sqlexpress  -> Shared memory
sqlcmd -Stcp:(local)\sqlexpress  ->  SQL Server Network Interfaces: Error Locating Server/Instance Specified

Can connect to (local)\sqlexpress from SQL Server Management Studio

So I am lost here. Any help is highly appreciated.

like image 947
Wullie Avatar asked Jul 03 '11 21:07

Wullie


People also ask

Can't connect to localhost on SQL Server?

Click the "IP Addresses" tab and scroll to the bottom. Under "IP All" remove TCP Dynamic Ports if it is present and set TCP Port to 1433. Click OK and then go back to "SQL Server Services" and restart SQL Server instance. Now you can connect via localhost, at least I could.

How do I connect to my localhost SQL database?

Connect to the SQL Server using SSMS Next, from the Connect menu under the Object Explorer, choose the Database Engine… Then, enter the information for the Server name (localhost), Authentication (SQL Server Authentication), and password for the sa user and click the Connect button to connect to the SQL Server.

How do I fix ODBC connection failed in SQL Server?

Resolving The Problem. When you create the ODBC connection to SQL server, select the option of With SQL Server authentication using a login ID and password entered by the user. Also, confirm that in Microsoft Access, the Machine Data Source and System Data Source has been used to create the ODBC connection.


2 Answers

in MS SQL, go to Configuration tools -> SQL Server Configuration Manager Select SQL Server Network Configuration -> Select protocol in the right side window enable tcp/ip and restart the services in services.msc then try to connect to localhost\sqlexpress

like image 102
Jonnadula Sasikumar Avatar answered Oct 02 '22 00:10

Jonnadula Sasikumar


Have you verified that Shared Memory is enabled in SQL Server Configuration Manager, under SQL Server Network Configuration | Protocols?

When I disable the "Shared Memory" protocol, "sqlcmd -S.\sqlexpress" times out. Enabling it makes the command work just fine.

One more thought... Port 1434 is used for the SQL Server Discovery service, which lets programs like SQL Management Studio and the like discover that SQL Server is running on the machine. Just one more thing to investigate.

like image 42
James L. Avatar answered Oct 02 '22 00:10

James L.