从电源 Shell 诊断 SQL 运行
Diagnose SQL running from Power Shell
我正在尝试从 Power Shell(在我的 windows 7 64 位桌面上)运行 SQL 并且远程数据库主机是 MS SQL 服务器 2012.
密码是:
$Server= ".\DB_HOST_NAME"
$Database = "master"
$UserSqlQuery= $("select count(*) from [master].[sys].[some_table]")
# executes a query and populates the $datatable with the data
function ExecuteSqlQuery ($Server, $Database, $SQLQuery) {
$Datatable = New-Object System.Data.DataTable
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
$Datatable.Load($Reader)
$Connection.Close()
$Datatable
return $Datatable
}
# declaration not necessary, but good practice
$resultsDataTable = New-Object System.Data.DataTable
$resultsDataTable = ExecuteSqlQuery $Server $Database $UserSqlQuery
Write-Host "Statistic: " $resultsDataTable
Write-Host "Message: Transaction Delay is " $resultsDataTable.Rows.Count
当我从 Windows PowerShell 运行 执行此操作时,出现以下错误:
Exception calling "Open" with "0" argument(s): "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: SQL Network Interfaces, error: 26
- Error Locating Server/Instance Specified)" At H:\test2.ps1:11 char:5
+ $Connection.Open()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Exception calling "ExecuteReader" with "0" argument(s): "ExecuteReader
requires an open and available Connection. The connection's current
state is closed." At H:\test2.ps1:15 char:5
+ $Reader = $Command.ExecuteReader()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
Exception calling "Load" with "1" argument(s): "Value cannot be null.
Parameter name: dataReader" At H:\test2.ps1:16 char:5
+ $Datatable.Load($Reader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
所以第一个错误是在 $Connection.Open()
但我对电源一无所知 shell 无法找出问题所在。我已经尝试 运行 在同一个数据库主机上使用相同的 SQL 并且它 return 一个数字。
我能得到一些帮助来了解我在这里做错了什么吗?
此代码:
$Server= ".\DB_HOST_NAME"
应该是:
$Server= "DB_HOST_NAME"
规范“.\DB_HOST_NAME”被解释为本地框上的命名实例 (DB_HOST_NAME)运行。
我正在尝试从 Power Shell(在我的 windows 7 64 位桌面上)运行 SQL 并且远程数据库主机是 MS SQL 服务器 2012.
密码是:
$Server= ".\DB_HOST_NAME"
$Database = "master"
$UserSqlQuery= $("select count(*) from [master].[sys].[some_table]")
# executes a query and populates the $datatable with the data
function ExecuteSqlQuery ($Server, $Database, $SQLQuery) {
$Datatable = New-Object System.Data.DataTable
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
$Datatable.Load($Reader)
$Connection.Close()
$Datatable
return $Datatable
}
# declaration not necessary, but good practice
$resultsDataTable = New-Object System.Data.DataTable
$resultsDataTable = ExecuteSqlQuery $Server $Database $UserSqlQuery
Write-Host "Statistic: " $resultsDataTable
Write-Host "Message: Transaction Delay is " $resultsDataTable.Rows.Count
当我从 Windows PowerShell 运行 执行此操作时,出现以下错误:
Exception calling "Open" with "0" argument(s): "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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" At H:\test2.ps1:11 char:5 + $Connection.Open() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SqlException
Exception calling "ExecuteReader" with "0" argument(s): "ExecuteReader requires an open and available Connection. The connection's current state is closed." At H:\test2.ps1:15 char:5 + $Reader = $Command.ExecuteReader() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidOperationException
Exception calling "Load" with "1" argument(s): "Value cannot be null. Parameter name: dataReader" At H:\test2.ps1:16 char:5 + $Datatable.Load($Reader) + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException
所以第一个错误是在 $Connection.Open()
但我对电源一无所知 shell 无法找出问题所在。我已经尝试 运行 在同一个数据库主机上使用相同的 SQL 并且它 return 一个数字。
我能得到一些帮助来了解我在这里做错了什么吗?
此代码:
$Server= ".\DB_HOST_NAME"
应该是:
$Server= "DB_HOST_NAME"
规范“.\DB_HOST_NAME”被解释为本地框上的命名实例 (DB_HOST_NAME)运行。