小编bin*_*shi的帖子

SQL 查询作为 Powershell 中的事务

我希望在 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
Run Code Online (Sandbox Code Playgroud)

代码:

try {
    #SQL Connection
    $Connection = New-Object System.Data.SQLClient.SQLConnection
    $Connection.ConnectionString = "Server=$server,1433;Initial Catalog=$Database;Persist Security Info=False;User ID=$UserID;Password=$Password;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;;"

    $Connection.Open()

    $Command = New-Object System.Data.SQLClient.SQLCommand
    $Command.Connection = $Connection

    #Begin Transaction
    $Connection.BeginTransaction()

    #INSERT …
Run Code Online (Sandbox Code Playgroud)

sql powershell

3
推荐指数
1
解决办法
4719
查看次数

标签 统计

powershell ×1

sql ×1