我想使用powershel运行查询。我想进行自动部署,而Powershell对我来说是一个挑战。因为我很陌生。我找到了一个可以部署SSIS包的查询,但是我需要使用powershell命令,因为我将从VSTS进行此操作。
查询为:
DECLARE @ProjectBinary AS varbinary(max)
DECLARE @operation_id AS bigint
SET @ProjectBinary = (SELECT * FROM OPENROWSET(BULK '<project_file_path>.ispac', SINGLE_BLOB) AS BinaryData)
EXEC catalog.deploy_project @folder_name = '<target_folder>',
@project_name = '<project_name',
@Project_Stream = @ProjectBinary,
@operation_id = @operation_id out
我尝试将代码插入以下示例
$dataSource = “.\SQLEXPRESS”
$user = “user”
$pwd = “1234”
$database = “Test”
$connectionString = “Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;”
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = “SELECT * FROM Person”
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
但是它没有用,我不确定自己在做什么错。我也尝试过关闭连接
$connection.Close() and nothing.
答案 0 :(得分:0)
创建连接对象
$sb = New-Object System.Data.Common.DbConnectionStringBuilder
$sb.set_ConnectionString($dbconnectionstring)
$serverName = $sb.server
$databaseName = $sb.database
$userName = $sb.'user id'
$password = $sb.password
我具有此功能,对验证SQL连接很有帮助
function Test-SQLConnection {
[OutputType([bool])]
Param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
$ConnectionString
)
try {
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString;
$sqlConnection.Open();
$sqlConnection.Close();
return $true;
}
catch {
return $false;
}
}
然后测试连接并使用Invoke-Sqlcmd
,因为您说它不接受$ cred,您可以尝试像这样直接传递$ userName $ pwd吗?
if(Test-SQLConnection $dbconnectionstring)
Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query $query -Username $username -Password $password