ArgumentOutOfRangeException,我不知道为什么
ArgumentOutOfRangeException and I can't figure out why
我正在构建一个 PowerShell 脚本,用于检索存储在 MS Access 2013 数据库文件中的一些信息。查询是根据机器上收集的一些其他数据动态构建的。我采用了完全模块化的方法,并将所有数据库功能和管理放在一个单独的模块中,我用 "Using Module" 语句引入该模块(功能相当大,所以我认为这会更容易管理代码)。除了 openRecordSet
函数外,一切似乎都运行良好。
这是我的数据库 class 以及我的 connectToDatabase
方法和 openRecordSet
方法的构造函数:
Database($dbPath) {
$this.connectToDatabase($dbPath)
}
connectToDatabase($Db) {
$this.databasePath = $Db
$this.connection = New-Object -ComObject ADODB.Connection
$this.connection.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source='$Db'")
if ($this.connection -eq $NULL -OR $this.connection.State -eq $this.adStateClosed) {
throw "$this.DatabasePath found but a valid connection could not be made."
}
}
[object] openRecordSet($query, $cursorType, $lockType) {
$RecordSet = New-Object -ComObject ADODB.Recordset
Write-Host "query = $query"
return $RecordSet.Open($query, $this.connection, $cursorType, $lockType)
}
这是给我带来问题的代码。它在另一个 class 中,在执行到达此行时已成功创建数据库对象和连接:
$RecSet = $this.database.openRecordSet($query, 3, 1)
使用Visual Studio代码的调试模式,一个Write-Host
语句和运行它在MS Access中,我已经确认SQL里面的语句$query
是正确的。第二个参数是游标类型,本例为Dynamic,第三个参数为锁类型,本例为ReadOnly。我使用的是我在数据库 class 中定义的常量,但将它们更改为原始数值以确认这些不会导致问题。错误没有改变。这是错误消息:
Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index
At C:\Users\FakeUser\Documents\MySuperScript\modules\userprofiles.psm1:20 char:21
+ ... $RecSet = $this.database.openRecordSet($query, 3, 1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentOutOfRangeException
+ FullyQualifiedErrorId : System.ArgumentOutOfRangeException
我不知道是什么导致了这个错误。 $query
是一个字符串,$this.connection
是一个 comObject 连接。我在不同的脚本中使用了相同的语法,没有任何问题,只有当我开始使用模块时才会发生这种情况。这肯定是一个简单的语法问题或其他一些我忽略的简单问题。
Open
方法的return类型无效。弹出错误是因为您正在将 void 转换为 object 以便 return 它。
这是 ADODB.Recordset
的列表
_xClone():ADODB.Recordset (Method)
_xResync(AffectRecords:ADODB.AffectEnum):System.Void (Method)
_xSave(FileName:System.String, PersistFormat:ADODB.PersistFormatEnum):System.Void (Method)
AbsolutePage:ADODB.PositionEnum (Get Property)
AbsolutePage:System.Void (Set Property)
AbsolutePosition:ADODB.PositionEnum (Get Property)
AbsolutePosition:System.Void (Set Property)
ActiveCommand:System.Object (Get Property)
ActiveConnection:System.Object (Get Property)
ActiveConnection:System.Void (Set Property)
AddNew(FieldList:System.Object, Values:System.Object):System.Void (Method)
BOF:System.Boolean (Get Property)
Bookmark:System.Object (Get Property)
Bookmark:System.Void (Set Property)
CacheSize:System.Int32 (Get Property)
CacheSize:System.Void (Set Property)
Cancel():System.Void (Method)
CancelBatch(AffectRecords:ADODB.AffectEnum):System.Void (Method)
CancelUpdate():System.Void (Method)
Clone(LockType:ADODB.LockTypeEnum):ADODB.Recordset (Method)
Close():System.Void (Method)
Collect:System.Object (Get Property)
Collect:System.Void (Set Property)
CompareBookmarks(Bookmark1:System.Object, Bookmark2:System.Object):ADODB.CompareEnum (Method)
CursorLocation:ADODB.CursorLocationEnum (Get Property)
CursorLocation:System.Void (Set Property)
CursorType:ADODB.CursorTypeEnum (Get Property)
CursorType:System.Void (Set Property)
DataMember:System.String (Get Property)
DataMember:System.Void (Set Property)
DataSource:System.Object (Get Property)
DataSource:System.Void (Set Property)
Delete(AffectRecords:ADODB.AffectEnum):System.Void (Method)
EditMode:ADODB.EditModeEnum (Get Property)
EOF:System.Boolean (Get Property)
Fields:ADODB.Fields (Get Property)
Filter:System.Object (Get Property)
Filter:System.Void (Set Property)
Find(Criteria:System.String, SkipRecords:System.Int32, SearchDirection:ADODB.SearchDirectionEnum, Start:System.Object):System.Void (Method)
GetRows(Rows:System.Int32, Start:System.Object, Fields:System.Object):System.Object (Method)
GetString(StringFormat:ADODB.StringFormatEnum, NumRows:System.Int32, ColumnDelimeter:System.String, RowDelimeter:System.String, NullExpr:System.String):System.String (Method)
Index:System.String (Get Property)
Index:System.Void (Set Property)
let_ActiveConnection(pvar:System.Object):System.Void (Method)
let_Source(pvSource:System.String):System.Void (Method)
LockType:ADODB.LockTypeEnum (Get Property)
LockType:System.Void (Set Property)
MarshalOptions:ADODB.MarshalOptionsEnum (Get Property)
MarshalOptions:System.Void (Set Property)
MaxRecords:System.Int32 (Get Property)
MaxRecords:System.Void (Set Property)
Move(NumRecords:System.Int32, Start:System.Object):System.Void (Method)
MoveFirst():System.Void (Method)
MoveLast():System.Void (Method)
MoveNext():System.Void (Method)
MovePrevious():System.Void (Method)
NextRecordset(RecordsAffected:System.Object&):ADODB.Recordset (Method)
Open(Source:System.Object, ActiveConnection:System.Object, CursorType:ADODB.CursorTypeEnum, LockType:ADODB.LockTypeEnum, Options:System.Int32):System.Void (Method)
PageCount:System.Int32 (Get Property)
PageSize:System.Int32 (Get Property)
PageSize:System.Void (Set Property)
Properties:ADODB.Properties (Get Property)
RecordCount:System.Int32 (Get Property)
Requery(Options:System.Int32):System.Void (Method)
Resync(AffectRecords:ADODB.AffectEnum, ResyncValues:ADODB.ResyncEnum):System.Void (Method)
Save(Destination:System.Object, PersistFormat:ADODB.PersistFormatEnum):System.Void (Method)
Seek(KeyValues:System.Object, SeekOption:ADODB.SeekEnum):System.Void (Method)
Sort:System.String (Get Property)
Sort:System.Void (Set Property)
Source:System.Object (Get Property)
Source:System.Void (Set Property)
State:System.Int32 (Get Property)
Status:System.Int32 (Get Property)
StayInSync:System.Boolean (Get Property)
StayInSync:System.Void (Set Property)
Supports(CursorOptions:ADODB.CursorOptionEnum):System.Boolean (Method)
Update(Fields:System.Object, Values:System.Object):System.Void (Method)
UpdateBatch(AffectRecords:ADODB.AffectEnum):System.Void (Method)
使用您的代码稍微修改示例:
Class Database {
[string] $databasePath
$connection
$adStateClosed
Database($dbPath){
$this.connectToDatabase($dbPath)
}
connectToDatabase($Db){
$this.databasePath = $Db
$this.connection = New-Object -ComObject ADODB.Connection
$this.connection.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source='$Db'" )
if($this.connection -eq $NULL -OR $this.connection.State -eq $this.adStateClosed){
Throw "$this.DatabasePath found but a valid connection could not be made."
}
}
[object] openRecordSet([string] $query, $cursorType, $lockType){
$RecordSet = new-object -ComObject ADODB.Recordset
$RecordSet.Open($query, $this.connection, $cursorType, $lockType)
return $RecordSet
}
}
$db = [Database]::new("C:\MonthlySalesReport\MonthlySalesReports.accdb")
$query = "SELECT 1 AS CONNECTED"
$result = $db.openRecordSet($query, 3, 1)
for ($i = 0; $i -lt $result.RecordCount; $i++) {
$record = $result[$i]
Write-Host ($record.Name + " " + $record.Value)
}
我正在构建一个 PowerShell 脚本,用于检索存储在 MS Access 2013 数据库文件中的一些信息。查询是根据机器上收集的一些其他数据动态构建的。我采用了完全模块化的方法,并将所有数据库功能和管理放在一个单独的模块中,我用 "Using Module" 语句引入该模块(功能相当大,所以我认为这会更容易管理代码)。除了 openRecordSet
函数外,一切似乎都运行良好。
这是我的数据库 class 以及我的 connectToDatabase
方法和 openRecordSet
方法的构造函数:
Database($dbPath) {
$this.connectToDatabase($dbPath)
}
connectToDatabase($Db) {
$this.databasePath = $Db
$this.connection = New-Object -ComObject ADODB.Connection
$this.connection.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source='$Db'")
if ($this.connection -eq $NULL -OR $this.connection.State -eq $this.adStateClosed) {
throw "$this.DatabasePath found but a valid connection could not be made."
}
}
[object] openRecordSet($query, $cursorType, $lockType) {
$RecordSet = New-Object -ComObject ADODB.Recordset
Write-Host "query = $query"
return $RecordSet.Open($query, $this.connection, $cursorType, $lockType)
}
这是给我带来问题的代码。它在另一个 class 中,在执行到达此行时已成功创建数据库对象和连接:
$RecSet = $this.database.openRecordSet($query, 3, 1)
使用Visual Studio代码的调试模式,一个Write-Host
语句和运行它在MS Access中,我已经确认SQL里面的语句$query
是正确的。第二个参数是游标类型,本例为Dynamic,第三个参数为锁类型,本例为ReadOnly。我使用的是我在数据库 class 中定义的常量,但将它们更改为原始数值以确认这些不会导致问题。错误没有改变。这是错误消息:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index At C:\Users\FakeUser\Documents\MySuperScript\modules\userprofiles.psm1:20 char:21 + ... $RecSet = $this.database.openRecordSet($query, 3, 1) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], ArgumentOutOfRangeException + FullyQualifiedErrorId : System.ArgumentOutOfRangeException
我不知道是什么导致了这个错误。 $query
是一个字符串,$this.connection
是一个 comObject 连接。我在不同的脚本中使用了相同的语法,没有任何问题,只有当我开始使用模块时才会发生这种情况。这肯定是一个简单的语法问题或其他一些我忽略的简单问题。
Open
方法的return类型无效。弹出错误是因为您正在将 void 转换为 object 以便 return 它。
这是 ADODB.Recordset
_xClone():ADODB.Recordset (Method)
_xResync(AffectRecords:ADODB.AffectEnum):System.Void (Method)
_xSave(FileName:System.String, PersistFormat:ADODB.PersistFormatEnum):System.Void (Method)
AbsolutePage:ADODB.PositionEnum (Get Property)
AbsolutePage:System.Void (Set Property)
AbsolutePosition:ADODB.PositionEnum (Get Property)
AbsolutePosition:System.Void (Set Property)
ActiveCommand:System.Object (Get Property)
ActiveConnection:System.Object (Get Property)
ActiveConnection:System.Void (Set Property)
AddNew(FieldList:System.Object, Values:System.Object):System.Void (Method)
BOF:System.Boolean (Get Property)
Bookmark:System.Object (Get Property)
Bookmark:System.Void (Set Property)
CacheSize:System.Int32 (Get Property)
CacheSize:System.Void (Set Property)
Cancel():System.Void (Method)
CancelBatch(AffectRecords:ADODB.AffectEnum):System.Void (Method)
CancelUpdate():System.Void (Method)
Clone(LockType:ADODB.LockTypeEnum):ADODB.Recordset (Method)
Close():System.Void (Method)
Collect:System.Object (Get Property)
Collect:System.Void (Set Property)
CompareBookmarks(Bookmark1:System.Object, Bookmark2:System.Object):ADODB.CompareEnum (Method)
CursorLocation:ADODB.CursorLocationEnum (Get Property)
CursorLocation:System.Void (Set Property)
CursorType:ADODB.CursorTypeEnum (Get Property)
CursorType:System.Void (Set Property)
DataMember:System.String (Get Property)
DataMember:System.Void (Set Property)
DataSource:System.Object (Get Property)
DataSource:System.Void (Set Property)
Delete(AffectRecords:ADODB.AffectEnum):System.Void (Method)
EditMode:ADODB.EditModeEnum (Get Property)
EOF:System.Boolean (Get Property)
Fields:ADODB.Fields (Get Property)
Filter:System.Object (Get Property)
Filter:System.Void (Set Property)
Find(Criteria:System.String, SkipRecords:System.Int32, SearchDirection:ADODB.SearchDirectionEnum, Start:System.Object):System.Void (Method)
GetRows(Rows:System.Int32, Start:System.Object, Fields:System.Object):System.Object (Method)
GetString(StringFormat:ADODB.StringFormatEnum, NumRows:System.Int32, ColumnDelimeter:System.String, RowDelimeter:System.String, NullExpr:System.String):System.String (Method)
Index:System.String (Get Property)
Index:System.Void (Set Property)
let_ActiveConnection(pvar:System.Object):System.Void (Method)
let_Source(pvSource:System.String):System.Void (Method)
LockType:ADODB.LockTypeEnum (Get Property)
LockType:System.Void (Set Property)
MarshalOptions:ADODB.MarshalOptionsEnum (Get Property)
MarshalOptions:System.Void (Set Property)
MaxRecords:System.Int32 (Get Property)
MaxRecords:System.Void (Set Property)
Move(NumRecords:System.Int32, Start:System.Object):System.Void (Method)
MoveFirst():System.Void (Method)
MoveLast():System.Void (Method)
MoveNext():System.Void (Method)
MovePrevious():System.Void (Method)
NextRecordset(RecordsAffected:System.Object&):ADODB.Recordset (Method)
Open(Source:System.Object, ActiveConnection:System.Object, CursorType:ADODB.CursorTypeEnum, LockType:ADODB.LockTypeEnum, Options:System.Int32):System.Void (Method)
PageCount:System.Int32 (Get Property)
PageSize:System.Int32 (Get Property)
PageSize:System.Void (Set Property)
Properties:ADODB.Properties (Get Property)
RecordCount:System.Int32 (Get Property)
Requery(Options:System.Int32):System.Void (Method)
Resync(AffectRecords:ADODB.AffectEnum, ResyncValues:ADODB.ResyncEnum):System.Void (Method)
Save(Destination:System.Object, PersistFormat:ADODB.PersistFormatEnum):System.Void (Method)
Seek(KeyValues:System.Object, SeekOption:ADODB.SeekEnum):System.Void (Method)
Sort:System.String (Get Property)
Sort:System.Void (Set Property)
Source:System.Object (Get Property)
Source:System.Void (Set Property)
State:System.Int32 (Get Property)
Status:System.Int32 (Get Property)
StayInSync:System.Boolean (Get Property)
StayInSync:System.Void (Set Property)
Supports(CursorOptions:ADODB.CursorOptionEnum):System.Boolean (Method)
Update(Fields:System.Object, Values:System.Object):System.Void (Method)
UpdateBatch(AffectRecords:ADODB.AffectEnum):System.Void (Method)
使用您的代码稍微修改示例:
Class Database {
[string] $databasePath
$connection
$adStateClosed
Database($dbPath){
$this.connectToDatabase($dbPath)
}
connectToDatabase($Db){
$this.databasePath = $Db
$this.connection = New-Object -ComObject ADODB.Connection
$this.connection.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source='$Db'" )
if($this.connection -eq $NULL -OR $this.connection.State -eq $this.adStateClosed){
Throw "$this.DatabasePath found but a valid connection could not be made."
}
}
[object] openRecordSet([string] $query, $cursorType, $lockType){
$RecordSet = new-object -ComObject ADODB.Recordset
$RecordSet.Open($query, $this.connection, $cursorType, $lockType)
return $RecordSet
}
}
$db = [Database]::new("C:\MonthlySalesReport\MonthlySalesReports.accdb")
$query = "SELECT 1 AS CONNECTED"
$result = $db.openRecordSet($query, 3, 1)
for ($i = 0; $i -lt $result.RecordCount; $i++) {
$record = $result[$i]
Write-Host ($record.Name + " " + $record.Value)
}