0

I need to check for general errors with my sql, like make sure it connects ok, make sure it returns with the query results ok, etc. I tried try/catch, but even though I know my login is wrong, it's not finding an error with the catch. I had found the try/catch idea at try catch.

How do I identify or catch when it doesn't logon to the db, or returns 0 results?

This is what I have so far:

function SQLQueryWriteToFile([string]$SQLquery, [string]$extractFile, [string]$facility)
{
   #create sql connection to connect to the sql DB

   try{
      $sqlConnection = New-Object System.Data.SqlClient.SqlConnection


      $sqlConnection.ConnectionString = "Server=blah;Database=blah;User ID=blah;Password=blah"  #sql connection
      $sqlConnection.Open()

      #Create a SqlCommand object to define the query
      $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
      $sqlCmd.CommandText = $SQLquery
      $sqlCmd.Parameters.Add('@facility',$facility)
      $sqlCmd.Connection = $sqlConnection

      #create a SqlAdapter that actually does the work and manages everything
      $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
      $sqlAdapter.SelectCommand = $sqlCmd
      $sqlAdapter.SelectCommand.CommandTimeout=300  #set timeout for query execution to 5 minutes (60x5=300)

      #create an empty dataSet for the query to fill with its results
      $dataSet = New-Object System.Data.DataSet

      #execute the query and fill the dataSet (then disconnect)
      $sqlAdapter.Fill($dataSet)
      $sqlConnection.Close()


      #dump the data to csv
      $DataSet.Tables[0] | Export-Csv $extractFile #this may not be comma delimited...need to check
   } #try
   catch{
      if($LASTEXITCODE -ne 0) #if there was an error, record it
      {
         Write-host $PSItem.ToString()
         $global:ErrorStrings.Add("Exception: $LASTEXITCODE ; $PSItem.ToString()  ")
      }
      else
      {
         $global:ErrorStrings.Add("Exception: $LASTEXITCODE ; $PSItem.ToString()  ")
      }
   } #catch
} 
9
  • 1
    As an aside: your try block doesn't call external programs, so there's no point in checking $LASTEXITCODE. Commented Dec 20, 2018 at 21:48
  • Which statement, specifically, triggers the error (that you think isn't caught)? Is it $sqlConnection.Open()? Commented Dec 20, 2018 at 21:48
  • 1
    After the Connection should be established, test if the connection works. Else throw your own exception. Commented Dec 21, 2018 at 13:48
  • 1
    See $sqlConnection.State Commented Dec 21, 2018 at 13:56
  • 1
    1.: Change the $ErrorActionPreference so that an Error will abort the iteration. 2: Make the parameter of the function mandatory 3: Some more throws :) Commented Dec 21, 2018 at 14:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.