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