Can't execute query to an Azure SQL Server from PowerShell

六月ゝ 毕业季﹏ 提交于 2020-01-17 07:15:07

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/45059125/cant-execute-query-to-an-azure-sql-server-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!