0

I have a SQL Azure database and I try to execute the query from PowerShell.

I am using Invoke-Sqlcmd Command.

I get this 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. 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)

But if I connect from Management Studio, everything is OK.

I setup a proper firewall rule.

Does anybody know what may happen?

Stephen King
5775 gold badges18 silver badges32 bronze badges
asked Jul 12, 2017 at 13:30
1
  • Can you share details of what parameters are you passing in the command. Commented Jul 12, 2017 at 14:31

1 Answer 1

3
Answer recommended by Microsoft Azure Collective

On your connection string, specify TCP as the protocol like

tcp:mymssqlserver.database.windows.net

Your PowerShell may look then like:

$server = " tcp:mymssqlserver.database.windows.net,1433" 
$database = "master" 
$adminName = "admin@my-azure-sql" 
$adminPassword = "P4SSW0rd" 
$connectionString = "Server=$server;Database=$database;User ID=$adminName;Password=$adminPassword;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" 
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
Pang
10.2k146 gold badges87 silver badges126 bronze badges
answered Jul 12, 2017 at 14:25
Sign up to request clarification or add additional context in comments.

1 Comment

In Alberto's answer, particularly important is the connection timeout setting, ensure it's 30 seconds or more.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.