April 16, 2010

Execute a SELECT and return a single value (scalar)

$UserNameToFind="Bill"
$connectionstring="Server=server.domain.com;Initial Catalog=MyDB;Trusted_Connection=yes"
$SQLCommandText="SELECT Usager_ID FROM T_Usager WHERE Netware_ID='$($UserNameToFind)' OR IDUL='$($UserNameToFind)'"
$SQLConnection = new-object system.data.sqlclient.SqlConnection($connectionstring)
$SQLConnection.open()
$sqlCommand = New-Object system.Data.sqlclient.SqlCommand($SQLCommandText,$SQLConnection)
$sqlCommand.ExecuteScalar()
$SQLConnection.close()

No comments:

Post a Comment