Can't connect to localhost on SQL Server Express 2012 / 2016
According to Aaron Bertand:
You need to verify that the SQL Server service is running. You can do this by going to
Start > Control Panel > Administrative Tools > Services
, and checking that the service SQL Server (SQLEXPRESS
) is running. If not, start it.While you're in the services applet, also make sure that the service SQL Browser is started. If not, start it.
-
You need to make sure that SQL Server is allowed to use TCP/IP or named pipes. You can turn these on by opening the SQL Server Configuration Manager in
Start > Programs > Microsoft SQL Server 2012 > Configuration Tools
(orSQL Server Configuration Manager
), and make sure that TCP/IP and Named Pipes are enabled. If you don't find the SQL Server Configuration Manager in the Start Menu you can launch the MMC snap-in manually. Check SQL Server Configuration Manager for the path to the snap-in according to your version. -
Verify your SQL Server connection authentication mode matches your connection string:
-
If you're connecting using a username and password, you need to configure SQL Server to accept "SQL Server Authentication Mode":
-- YOU MUST RESTART YOUR SQL SERVER AFTER RUNNING THIS! USE [master] GO DECLARE @SqlServerAndWindowsAuthenticationMode INT = 2; EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, @SqlServerAndWindowsAuthenticationMode; GO
- If you're connecting using "Integrated Security=true" (Windows Mode), and this error only comes up when debugging in web applications, then you need to add the ApplicationPoolIdentity as a SQL Server login:
-
otherwise, run
Start -> Run -> Services.msc
If so, is it running?
If it's not running then
It sounds like you didn't get everything installed. Launch the install file and chose the option "New installation or add features to an existing installation". From there you should be able to make sure the database engine service gets installed.
Goto Start -> Programs -> Microsoft SQL ServerYYYY -> Configuration Tools -> SQL Server YYYY Configuration Manager or run "SQLServerManager12.msc".
Make sure that TCP/IP is enabled under Client Protocols.
Then go into "SQL Server Network Configuration" and double click TCP/IP. 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.
Note that this error can of course occur when connecting from other applications as well. Example for a normal C# web application Web.config
connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
in SQL SERVER EXPRESS 2012 you should use "(localdb)\MSSQLLocalDB" as Data Source name for example you can use connection string like this
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;