尝试将值保存到 Access 数据库,但在尝试 ExecuteNonQuery 时出现错误
Trying to save values to an Access DB, but I'm getting an error when I try ExecuteNonQuery
我正在尝试使用 Powershell 从 MS Access 数据库中提取 PC 名称列表,提取它们的驱动器大小统计信息(WMI 对象),然后将它们保存回 Access。我不断收到以下错误:
Exception calling "ExecuteNonQuery" with "0" argument(s): "No value given
for one or more required parameters."
At C:\Users\*******\Desktop\Scripts\AccessUpdate\HDDUpdate.ps1:75 char:5
+ $WriteFreeSpaceCmd.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException
这是 运行 Windows 10,Access 2016 x64,使用 Visual Studio 代码调试,PowerShell 5.1,NET Framework 4.8,ADE 2016 x64。
我已经检查了所有字段名称,但我不知道该检查什么。我的Google-fu也没用。
$connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jharold\Desktop\Scripts\AccessUpdate\WinHDDTest.accdb;Persist Security Info=False;" #Change Source to DB location
[reflection.assembly]::LoadWithPartialName("System.Data") | Out-Null
$Conn = New-Object System.Data.OleDb.OleDbConnection
$Conn.ConnectionString = $connstr
$Conn.Open() | Out-Null
#
# Pull PC names from db and gather HDD info code works fine
#
foreach ($PC in $HDDInfo) {
$WriteFreeSpaceCmd = New-Object System.Data.OleDb.OleDbCommand
$WriteFreeSpaceCmd.Connection = $Conn
$WriteTotalSpaceCmd = New-Object System.Data.OleDb.OleDbCommand
$WriteTotalSpaceCmd.Connection = $Conn
$temp1 = "UPDATE Status SET ImportFreeSpace = " + $PC.FreeSpace + " WHERE NetBios_Name = " + $PC.NetBiosInfo
$WriteFreeSpaceCmd.CommandText = $temp1
$temp2 = "UPDATE Status SET ImportTotalSpace = " + $PC.TotalSpace + " WHERE NetBios_Name = " + $PC.NetBiosInfo
$WriteTotalSpaceCmd.CommandText = $temp2
$WriteFreeSpaceCmd.ExecuteNonQuery()
$WriteTotalSpaceCmd.ExecuteNonQuery()
}
我希望$PC.FreeSpace和$PC.TotalSpace以文本形式保存到相应PC的ImportFreeSpace和ImportTotalSpace。相反,它一运行 ExecuteNonQuery() 就会出错。
UPDATE 命令中的值应该用引号引起来。喜欢:
$temp1 = "UPDATE Status SET ImportFreeSpace = '{0}' WHERE NetBios_Name = '{1}' " -f $PC.FreeSpace, $PC.NetBiosInfo
我正在尝试使用 Powershell 从 MS Access 数据库中提取 PC 名称列表,提取它们的驱动器大小统计信息(WMI 对象),然后将它们保存回 Access。我不断收到以下错误:
Exception calling "ExecuteNonQuery" with "0" argument(s): "No value given
for one or more required parameters."
At C:\Users\*******\Desktop\Scripts\AccessUpdate\HDDUpdate.ps1:75 char:5
+ $WriteFreeSpaceCmd.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException
这是 运行 Windows 10,Access 2016 x64,使用 Visual Studio 代码调试,PowerShell 5.1,NET Framework 4.8,ADE 2016 x64。
我已经检查了所有字段名称,但我不知道该检查什么。我的Google-fu也没用。
$connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jharold\Desktop\Scripts\AccessUpdate\WinHDDTest.accdb;Persist Security Info=False;" #Change Source to DB location
[reflection.assembly]::LoadWithPartialName("System.Data") | Out-Null
$Conn = New-Object System.Data.OleDb.OleDbConnection
$Conn.ConnectionString = $connstr
$Conn.Open() | Out-Null
#
# Pull PC names from db and gather HDD info code works fine
#
foreach ($PC in $HDDInfo) {
$WriteFreeSpaceCmd = New-Object System.Data.OleDb.OleDbCommand
$WriteFreeSpaceCmd.Connection = $Conn
$WriteTotalSpaceCmd = New-Object System.Data.OleDb.OleDbCommand
$WriteTotalSpaceCmd.Connection = $Conn
$temp1 = "UPDATE Status SET ImportFreeSpace = " + $PC.FreeSpace + " WHERE NetBios_Name = " + $PC.NetBiosInfo
$WriteFreeSpaceCmd.CommandText = $temp1
$temp2 = "UPDATE Status SET ImportTotalSpace = " + $PC.TotalSpace + " WHERE NetBios_Name = " + $PC.NetBiosInfo
$WriteTotalSpaceCmd.CommandText = $temp2
$WriteFreeSpaceCmd.ExecuteNonQuery()
$WriteTotalSpaceCmd.ExecuteNonQuery()
}
我希望$PC.FreeSpace和$PC.TotalSpace以文本形式保存到相应PC的ImportFreeSpace和ImportTotalSpace。相反,它一运行 ExecuteNonQuery() 就会出错。
UPDATE 命令中的值应该用引号引起来。喜欢:
$temp1 = "UPDATE Status SET ImportFreeSpace = '{0}' WHERE NetBios_Name = '{1}' " -f $PC.FreeSpace, $PC.NetBiosInfo