简体   繁体   English

无法从PowerShell对Azure SQL Server执行查询

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

I have a SQL Azure database and I try to execute the query from PowerShell. 我有一个SQL Azure数据库,我尝试从PowerShell执行查询。

I am using Invoke-Sqlcmd Command. 我正在使用Invoke-Sqlcmd命令。

I get this error message: 我收到此错误消息:

A network related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

But if I connect from Management Studio, everything is OK. 但是,如果我从Management Studio连接,一切正常。

I setup a proper firewall rule. 我设置了正确的防火墙规则。

Does anybody know what may happen? 有人知道会发生什么吗?

On your connection string, specify TCP as the protocol like 在您的连接字符串上,将TCP指定为协议,例如

tcp:mymssqlserver.database.windows.net

Your PowerShell may look then like: 然后,您的PowerShell可能如下所示:

$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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 Powershell 执行测试 - Can't execute test from Powershell SQL Server 2008 R2-从Powershell执行维护计划 - SQL Server 2008 R2 - Execute Maintenance Plan from Powershell 可以从其他计算机的 SSMS 连接到 SQL Server,但无法从 Powershell 连接到网络中的 SQL Server - Can connect to SQL Server from SSMS from other computer but Can't connect to SQL Server in a network from powershell 尝试使用Powershell从Azure还原数据库时,八达通部署找不到SQL凭据 - Octopus deploy can't find SQL Credential when trying to restore a database from Azure using powershell 使用Azure Powershell执行.sql文件 - Use Azure Powershell to execute a .sql file 如何从 Azure Powershell 运行手册连接到本地 SQL 服务器? - How to connect to an on-premise SQL Server from Azure Powershell runbook? 无法使用PowerShell在Azure SQL数据库上设置标记 - Can't set a tag on an Azure SQL Database using PowerShell 从PowerShell执行SQL * Plus - Execute SQL*Plus from PowerShell 我正在尝试通过 powershell 在我的 Azure 订阅中获取所有 Postgre SQl 数据库的列表,但找不到查询 - I'm trying to get a list of all Postgre SQl Databases in my Azure subscription via powershell, but can't find a query for it 无法将Powershell 5.0脚本作为Azure Webjob执行 - Can not execute powershell 5.0 script as azure webjob
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM