PoshCode Archive  Artifact [70e1725be2]

Artifact 70e1725be2fe95bfd1f3e9c80ff0b8e9e7c2bec1207db1d0586ea8ecc71136e9:

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

# 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: 479
# x-archived: 2014-08-01T20:44:30
# x-published: 2008-07-24T09:17: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}
}