sqlcmd - Error: Microsoft ODBC Driver 17 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server

By xngo on September 9, 2021

Issue

I used to use this sqlcmd to run my query and it is working fine up until I ran it in a different computer. I got the following error:

>sqlcmd -S localhost -d master -E -h -1 -i my-query.sql
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server 
[2]. .
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-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 if 
SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

Solution

Specify the exact SQL Server named instance instead of using localhost. So, I change from sqlcmd -S localhost...tosqlcmd -S .\SQLEXPRESS...`.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.