MyITGuy
3/11/2017 - 9:21 PM

PowerShell: Connect to a SQLite Database

PowerShell: Connect to a SQLite Database

Add-Type -Path "$PSScriptRoot\System.Data.SQLite.dll"

$DataSource = "$PSScriptRoot\[sqlite_database_file]]"
$CommandText = "select * from mytable"

$SQLiteConnection = New-Object System.Data.SQLite.SQLiteConnection
$SQLiteConnection.ConnectionString = "DataSource=$DataSource"
$SQLiteConnection.Open()

$SQLiteCommand = $SQLiteConnection.CreateCommand()
$SQLiteCommand.CommandText = $CommandText

$SQLiteDataAdapter = New-Object System.Data.SQLite.SQLiteDataAdapter
$SQLiteDataAdapter.SelectCommand = $SQLiteCommand

$DataTable = New-Object System.Data.DataTable
[void]$SQLiteDataAdapter.Fill($DataTable)
$SQLiteConnection.Close()
$DataTable