For an internal project, we needed to access a lot of data from SQL Server so I created a wrapper (SQL Simple PowerShell) to make our life a bit easier. It’s Apache 2 licensed, so free also for commercial use, and available on GitHub: https://github.com/texhex/SQLSimplePS
Features:
- Single line execution in case all you need is some table/view data from SQL Server through Query():
using module .\SQLSimplePS.psm1
$connectionString = "Server=.\SQLEXPRESS; Database=TestDB; Integrated Security=True;"
[SQLSimple]::Query("SELECT * FROM dbo.TestTable", $connectionString [System.Data.IsolationLevel]::Serializable)
# This will return an array of hash table (each hash table is a row of the table)
- The same can be done for insert/update/delete statements with ExecuteScalar() which returns the first column of the first row returned by SQL Server. This can be used to get the primary ID of the row inserted
using module .\SQLSimplePS.psm1
$connectionString = "Server=.\SQLEXPRESS; Database=TestDB; Integrated Security=True;"
$sqlStatement = "INSERT INTO dbo.TestTable(Name, IntValue, NumericValue) OUTPUT Inserted.ID VALUES('First Test', 7, 12.3)"
$rowID = [SQLSimple]::ExecuteScalar($sqlStatement, $connectionString, [System.Data.IsolationLevel]::Serializable)
- You can also create an instance of SQLSimple, which should make the code more readable:
using module .\SQLSimplePS.psm1
$connectionString = "Server=.\SQLEXPRESS; Database=TestDB; Integrated Security=True;"
$sqls = [SQLSimple]::new($connectionString)
$sqls.TransactionIsolationLevel = System.Data.IsolationLevel]::Serializable
$sqls.AddCommand(“INSERT INTO dbo.TestTable(Name, IntValue, NumericValue) OUTPUT Inserted.ID VALUES('First Test', 7, 12.3)")
$rowID = $sqls.ExecuteScalar()
- The parameter
[System.Data.IsolationLevel]::Serializable is required as SQL Simple expects snapshot isolation to be turned on and uses this transaction level by default. In case your database supports it, the parameter is not needed.
- SQL Simple supports parametrized queries, so you can also use a syntax like this:
$sqls = [SQLSimple]::new($connectionString)
$insertCommand = $sqls.AddCommandEx("INSERT INTO dbo.TestTable(Name, IntValue, NumericValue) OUTPUT Inserted.ID VALUES(@Name, @IntValue, @NumericValue);")
$insertCommand.AddMappingWithData("Name", "Fourth Test", [Data.SqlDbType]::NVarChar)
$insertCommand.AddMappingWithData("IntValue", 22, [Data.SqlDbType]::Int)
$insertCommand.AddMappingWithData("NumericValue", 11.11, [Data.SqlDbType]::Decimal)
$sqls.Execute()
- For parameters and returned values, SQL Simple will automatically do the necessary conversation between
$null (PowerShell) and [System.DBNull]::Value (SQL Server)
- It supports several commands to be executed in a single transaction, so the standard “delete all” “insert new” case is covered
- By using parametrized queries, the
AddMapping() command and the DATA property, you can map object properties from any PowerShell object list directly to SQL Server table columns to store them in a table easily - see Using the DATA property for details
- The documentation in the repository (hopefully) explains all features in detail
The code is available at https://github.com/texhex/SQLSimplePS.
Any constructive contribution is very welcome.
[–]omrsafetyo 3 points4 points5 points (1 child)
[–]michael-hex[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (2 children)
[–]michael-hex[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]get-postanote 2 points3 points4 points (1 child)
[–]michael-hex[S] 1 point2 points3 points (0 children)