Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AccessDB {
- hidden [System.__ComObject]$Connection
- [string]$DBPath
- AccessDB([string]$Path) {
- if (-not (Test-Path $Path)) {
- throw "Invalid Access database path specified ($Path). Please supply full absolute path to database file!"
- }
- $FullPath = (Resolve-Path $Path).ProviderPath
- $this.DBPath = $FullPath
- $this.Connection = New-Object -ComObject ADODB.Connection
- try {
- $this.Connection.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$FullPath`";Persist Security Info=False;")
- } catch {
- $this.Connection = $null
- }
- }
- }
- class AccessRS {
- hidden [System.__ComObject]$RecordSet
- [System.Collections.Generic.List`1[PSCustomObject]]$Values = @()
- [string]$Query
- [AccessDB]$DB
- AccessRS([AccessDB]$Conn, [string]$Query ) {
- [int]$CursorType = 3
- [int]$LockType = 3
- $this.DB = $Conn
- $this.Query = $Query
- $this.RecordSet = New-Object -ComObject ADODB.Recordset
- Write-Verbose "$Query, $($this.DB.Connection.Provider), $CursorType, $LockType"
- $this.RecordSet.Open($Query, $this.DB.Connection, $CursorType, $LockType)
- $fields = $this.GetAccessRecordSetStructure()
- try { $this.RecordSet.MoveFirst() } catch { return }
- do {
- $record = @{ }
- foreach ($field in $fields) {
- $record[$field.Name] = $this.RecordSet.Fields.Item($field.Name).Value
- }
- $this.Values.Add( ([PSCustomObject]$record))
- $this.RecordSet.MoveNext()
- } until ($this.RecordSet.EOF -eq $True)
- }
- [boolean]Test() {
- if (-not $this.RecordSet) { return $false }
- return ( $this.RecordSet.RecordCount -gt 0 )
- }
- [System.Collections.Generic.List`1[PSCustomObject]]GetAccessRecordSetStructure() {
- [System.Collections.Generic.List`1[PSCustomObject]]$Fields = @()
- $this.Recordset.Fields | % {
- $Fields.Add( ([PSCustomObject]@{
- Name = $_.Name
- Attributes = $_.Attributes
- DefinedSize = $_.DefinedSize
- Type = $_.Type
- }))
- }
- return $Fields
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement