verify a sql server connection

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6388

    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?

    Thanks

    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”
    $SqlConnection.Open()
    if($SqlConnection.state -eq “Open”)
    {
    echo “$sqlserver – Connection established db=$SqlCatalog : $(get-date -format yyyy.MM.dd-HH.mm.ss)” >> $FileLog
    #c:\servers.out
    $SqlConnection.Close()
    echo “Succes”
    }
    else
    {
    echo “$sqlserver – No connection db=$SqlCatalog : $(get-date -format yyyy.MM.dd-HH.mm.ss)” >> $FileLog
    }

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

You must be logged in to reply to this topic.