PoshCode Archive  Artifact [a0b5c897ef]

Artifact a0b5c897ef5b14af8ff651bb41c4480be12f51eae869771780b184e26855f602:

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

# 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: 2579
# x-archived: 2017-03-25T18:32:47
# x-published: 2011-03-26T05:50: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 = [System.Data.CommandType]::Text
	$Command.Connection = $sqlConnection
	
	$sqlConnection.Open()
	$Result = $Command.ExecuteNonQuery()
	$sqlConnection.Close()
	
	if ($Result -gt 0) {return $TRUE} else {return $FALSE}
}