mithrandyr / SimplySql

PowerShell module for querying various SQL databases
MIT License
202 stars 30 forks source link

Open-SQLiteConnection does not open connection when using ConnectionName parameter #19

Closed IonutDanNica closed 5 years ago

IonutDanNica commented 5 years ago

I'm pretty new to SimplySQL but I can't seem to be able to open a connection to a database file when using the ConnectionName parameter. Here's an example

$FilePath = "C:\Users\inica\Documents\result.db"

Open-SQLiteConnection -FilePath $FilePath -ConnectionName foo
Get-SqlConnection
WARNING: There is no active SQL Connection.

When using the commandlet without the connectionName paramter a connection is opened

$FilePath = "C:\Users\inica\Documents\result.db"

Open-SQLiteConnection -FilePath $FilePath 
Get-SqlConnection

PoolCount         : 0
ConnectionString  : Data Source=C:\Users\inica\Documents\result.db
DataSource        : result
FileName          : C:\Users\inica\Documents\result.db
Database          : main
DefaultTimeout    : 30
BusyTimeout       : 0
WaitTimeout       : 30000
PrepareRetries    : 3
ProgressOps       : 0
ParseViaFramework : False
Flags             : Default
DefaultDbType     : 
DefaultTypeName   : 
VfsName           : 
OwnHandle         : True
ServerVersion     : 3.24.0
LastInsertRowId   : 0
Changes           : 0
AutoCommit        : True
MemoryUsed        : 401202
MemoryHighwater   : 401202
State             : Open
ConnectionTimeout : 15
Site              : 
Container         : 

Is this normal, or am I doing something wrong?

mithrandyr commented 5 years ago

@IonutDanNica -- the command Get-SqlConnection is rarely used... only necessary when you need to drop into pure .Net and want the underlying Connection Object. That being said... Whenever you create a connection Open-*Connection and use the parameter -ConnectionName, then for any other command you want to use with that connection, you must pass in the ConnectionName. For example:

$FilePath = "C:\Users\inica\Documents\result.db"

Open-SQLiteConnection -FilePath $FilePath -ConnectionName foo
Show-SqlConnection -ConnectionName foo
Close-SqlConnection -cn foo #using the alias for ConnectionName which is CN

Open-SqlConnection #openning MSSQL connection with all default parameters
Show-SqlConnection
Show-SqlConnection -CN default # this will do the same thing.. because when you open a connection and don't specify -ConnectionName, then the name 'default' is assigned automatically.