PoshCode Archive  Artifact [c761692c22]

Artifact c761692c221be22f087192b5c0d1664f0dd6c73c2737a9f08a05253f387d3c49:

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

# 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: 481
# x-derived-from-id: 2579
# x-archived: 2015-05-06T20:28:07
# x-published: 2009-07-24T09:20: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=user;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}
}