PoshCode Archive  Artifact [585d605c2a]

Artifact 585d605c2a8ee15c30f5185c3ff6fc386d056dc5422826708dacd0a44d74b4a2:

  • File Execute-SQLCommand.ps1 — part of check-in [2da2a60210] at 2018-06-10 13:46:00 on branch trunk — Simple function that executes a command (stored procedure) against an SQL database. (user: dragonmc77 size: 1111)

# encoding: ascii
# api: powershell
# title: Execute-SQLCommand
# description: Simple function that executes a command (stored procedure) against an SQL database.
# version: 0.1
# type: function
# author: dragonmc77
# license: CC0
# function: Execute-SQLCommand
# x-poshcode-id: 480
# x-derived-from-id: 481
# x-archived: 2016-10-13T05:23:24
# x-published: 2008-07-24T09:18:00
#
#
function Execute-SQLCommand {param(	[string]$Server,				#the host name of the SQL server
					[string]$Database,				#the name of the database
					[System.Data.SqlClient.SqlCommand]$Command)	#the command to execute (name of stored procedure)

	$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
	$sqlConnection.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;User ID=ml;Initial Catalog=$Database;Data Source=$Server"
	
	$Command.CommandType = 1 # 1 is the 'Text' command type
	$Command.Connection = $sqlConnection
	
	$sqlConnection.Open()
	$Result = $Command.ExecuteNonQuery()
	$sqlConnection.Close()
	
	if ($Result -gt 0) {return $TRUE} else {return $FALSE}
}