Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询作为Powershell中的事务_Sql_Powershell - Fatal编程技术网

SQL查询作为Powershell中的事务

SQL查询作为Powershell中的事务,sql,powershell,Sql,Powershell,我希望在PowerShell中作为事务执行一些SQL查询。然而,我得到了错误,无法找到一个合适的答案。 除了错误之外,BeginTransaction()和CommitTransaction()是在Powershell中执行事务的方式吗?Intellisense似乎没有显示CommitTransaction()方法 错误: Exception calling "ExecuteNonQuery" with "0" argument(s): "ExecuteNonQuery requires the

我希望在PowerShell中作为事务执行一些SQL查询。然而,我得到了错误,无法找到一个合适的答案。 除了错误之外,BeginTransaction()和CommitTransaction()是在Powershell中执行事务的方式吗?Intellisense似乎没有显示CommitTransaction()方法

错误:

Exception calling "ExecuteNonQuery" with "0" argument(s): "ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been 
initialized
代码:


您必须将
SqlCommand
Transaction
属性设置为
BeginTransaction
返回的事务对象:

$command.Transaction = $connection.BeginTransaction()
try {
    $command.ExecuteNonQuery()
    $command.Transaction.Commit()
}
catch {
    $command.Transaction.Rollback()
    throw
}

请编辑问题并添加错误消息。@vonPryz错误在代码部分之前
$command.Transaction = $connection.BeginTransaction()
try {
    $command.ExecuteNonQuery()
    $command.Transaction.Commit()
}
catch {
    $command.Transaction.Rollback()
    throw
}