1

I have the following code running, which should basically just execute a SQL statement against my database and if there is any error, the script should shout "ERROR!"

When I am running the script, with an invalid SQL statement in the test.sql file, the exception is correctly caught by the CATCH clause and "ERROR!" is written.

Test.sql file looks like this:

SELECT * FRRRROM [dbo].[testtable]

But, when I add a comment to the test.sql file, it doesn't throw the "ERROR!"-message anymore.

Test.sql file now looks like this:

-- my comment
SELECT * FRRRROM [dbo].[testtable]

I don't get it why. Any suggestions?

$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='myServer';database='myDatabase';trusted_connection=true;MultipleActiveResultSets=True;"
$Connection.Open()

$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection 

$sql = Get-Content 'C:\Test\test.sql'

$Command.CommandText = $sql
Try
{
    $Reader = $Command.ExecuteReader()
}
Catch 
{
    echo 'ERROR!!!!'
    Exit
}
$Connection.Close()
2
  • 1
    try $sql = Get-Content 'C:\Test\test.sql' | Out-String Do you get an error then? Difference in your second example $sql is an array and not just a newline delimited string. Commented Oct 7, 2015 at 15:12
  • Thank you very much! Solved the problem. Didn't knew that my example returned an array! Thx again! you made my day! Commented Oct 7, 2015 at 15:22

2 Answers 2

2

I still would have expected an error but the main difference between your first and second example is you have another line. Get-Content returns a string array. With one line it would be just seen as a string. Easy way to be sure of that is to convert the text into one whole new line delimited string.

$sql = Get-Content 'C:\Test\test.sql' | Out-String

You could use -join or something else like that but I find Out-String is very simple and compatible with all versions of PowerShell.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again! additional question: Whats the best way to determine the error risen? reading the SQL Code of the execution. Sorry, i'm quite new into that topic
@EstebanP. Inside your catch you could look at $error[0].Exception which contains the exception message captured. Could be vague though. It depends on what System.Data.SQLClient.SQLCommand is willing to explain
0

As user Matt mentioned:

The code returns an array to the variable $sql

$sql = Get-Content 'C:\Test\test.sql'
$Command.CommandText = $sql

after adding | Out-String it's working correctly, as returning a string

$Command.CommandText = Get-Content 'C:\Test\test.sql'| Out-String

Comments

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.