sql-server - Powershell SQL Server 更新查询

标签 sql-server powershell

我正在尝试连接到 Microsoft SQL 数据库并将已更改字段 = 的任何记录更新为“x”。我可以查询数据库,但是当我尝试进行更新时,出现此错误:

Fill : Exception calling "Fill" with "1" argument(s): "Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding."

这是我正在使用的脚本:

#Create SQL Connection
$con = new-object "System.data.sqlclient.SQLconnection"

#Set Connection String
$con.ConnectionString =(“Data Source=server;Initial Catalog=IDCards;Integrated Security=SSPI”)
$con.open()

$sqlcmd = new-object "System.data.sqlclient.sqlcommand"
$sqlcmd.connection = $con
$sqlcmd.CommandTimeout = 600000
#$sqlcmd.CommandText = “select * from tblPhotoID where changed = 'X'”
$sqlcmd.CommandText = “UPDATE dbo.tblPhotoID SET Changed = '1' WHERE Changed ='X'”
$adapter = New-Object system.data.sqlclient.sqldataadapter ($sqlcmd.CommandText, $con)
$set = New-Object system.data.dataset
$adapter.Fill($set)

目前大约有 4000 条记录需要更新。该脚本运行大约 30 秒后超时。我尝试增加命令超时并得到相同的结果。

最佳答案

您的更新语句不会返回记录集,因此没有任何内容可以填充数据集。相反,您想尝试以下操作:

#Create SQL Connection
$con = new-object "System.data.sqlclient.SQLconnection"

#Set Connection String
$con.ConnectionString =(“Data Source=sb-inft-orange.ads.iu.edu;Initial Catalog=IDCards;Integrated Security=SSPI”)
$con.open()

$sqlcmd = new-object "System.data.sqlclient.sqlcommand"
$sqlcmd.connection = $con
$sqlcmd.CommandTimeout = 600000
$sqlcmd.CommandText = “UPDATE dbo.tblPhotoID SET Changed = '1' WHERE Changed ='X'”
$rowsAffected = $sqlcmd.ExecuteNonQuery()

关于sql-server - Powershell SQL Server 更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362793/

相关文章:

powershell - 将Elasticsearch 5.6.4作为Windows服务安装时出错

json - 使用 PowerShell 将 Chrome 书签导出到 CSV 文件

sql-server - 如何在 Management Studio 中编写参数化查询?

sql - 当连接关闭时,未提交的事务会发生什么?

sql - 无法将 SSIS 项目部署到 SQL Server

powershell - Powershell 是 Windows 开发人员必备的技能吗?比如 Bash/Borne 是 Linux/UNIX 开发人员的必备技能吗?

windows - 在PowerShell中调用同目录音频文件

powershell - 如何获取 TFS 本地工作区中的项目计数?

c# - 尝试向数据库中插入行时 SQL 语法无效

sql-server - 如何查找sql​​ server ro14中的死锁原因?