verify a sql server connection

by antonela at 2013-03-22 03:42:02

I use sqlcmd -S $Server -d db -b -Q “EXEC pr_prova” >>"$FileLog"
in a file.ps1, but before to do this command I would like to verify if the connection was successfully established with sql server.

If so, I go ahead and launch my stored procedure using my sqlcmd-…,
If not, I want to exit from my file.ps1 (and return an errorr). How can I do to write in a log file ($FileLog) that the connection with db was\wasn’t ok?

At the end of a cmd I used exit /B %errorlevel% to return the result of my job.This value is get in input from another software which uses it to set an indicator.
If I use $? can I have the same result?

by antonela at 2013-03-22 06:19:49
I found:

$SqlServer =“SQL_S”
$SqlCatalog = “master”
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = “Server = $SqlServer; Database = $SqlCatalog; Integrated Security = True”
if($SqlConnection.state -eq “Open”)
echo “$sqlserver – Connection established db=$SqlCatalog : $(get-date -format” >> $FileLog
echo “Succes”
echo “$sqlserver – No connection db=$SqlCatalog : $(get-date -format” >> $FileLog

But how could I read the value of my Sqlserver ($SqlServer) from an .ini file?