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()
$sql = Get-Content 'C:\Test\test.sql' | Out-StringDo you get an error then? Difference in your second example$sqlis an array and not just a newline delimited string.