无法从PowerShell执行对Azure SQL Server的查询 [英] Can't execute query to an Azure SQL Server from PowerShell
问题描述
我有一个SQL Azure数据库,我尝试从PowerShell执行查询.
I have a SQL Azure database and I try to execute the query from PowerShell.
我正在使用Invoke-Sqlcmd
命令.
我收到此错误消息:
与网络相关或特定于实例的错误发生在 建立与SQL Server的连接.找不到服务器或 无法访问.验证实例名称正确,并且 SQL Server配置为允许远程连接. (提供者:命名 Pipes Provider,错误:40-无法打开与SQL Server的连接)
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)
但是,如果我从Management Studio连接,一切正常.
But if I connect from Management Studio, everything is OK.
我设置了正确的防火墙规则.
I setup a proper firewall rule.
有人知道会发生什么吗?
Does anybody know what may happen?
推荐答案
在连接字符串上,将TCP指定为协议,例如
On your connection string, specify TCP as the protocol like
tcp:mymssqlserver.database.windows.net
您的PowerShell可能如下所示:
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)
这篇关于无法从PowerShell执行对Azure SQL Server的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!