liveBook · Manning

System.Data.SqlClient.SqlCommand

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

System.Data.SqlClient.SqlCommand is a .NET class that represents a SQL statement or command executed through a database connection.

System.Data.SqlClient.SqlCommand represents a SQL statement executed through an open SQL Server connection. Set its Connection and CommandText properties, then call ExecuteNonQuery() when the statement returns no rows. Suppress the method’s successful -1 result with Out-Null. Keeping SQL in a here-string makes formatting, variable insertion, and verbose syntax checking easier. section 26.3

Create a table from PowerShell

New-Object System.Data.SqlClient.SqlCommand creates the command, which receives an opened SqlConnection and a here-string containing conditional CREATE TABLE SQL. ExecuteNonQuery() runs the definition, and Close() releases the connection afterward.

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains connection, CommandText, here-strings, ExecuteNonQuery(), result suppression, and cleanup through a complete table-creation function.
Examples
Uses New-DiskInfoSQLTable to create a diskinfo table with SQL Server column types and a Windows-authenticated connection string.
What it assumes
Assumes PowerShell and SQL Server familiarity while demonstrating connection opening, SQL construction, command execution, and closure.
What you can do after
Operate/configure a PowerShell database function that creates a SQL Server table and suppresses nonquery output.
How it connects
Connects SQL commands with SqlConnection, PowerShell here-strings, ExecuteNonQuery(), and SQL Server CREATE TABLE statements.

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.