Consider this simple SQL script:
PRINT N'Dropping CREATE_TABLE events from DatabaseLog table...'
DELETE FROM [dbo].[DatabaseLog] WHERE Event = N'CREATE_TABLE'
PRINT N'Dropping ALTER_TABLE events from DatabaseLog table...'
DELETE FROM [dbo].[DatabaseLog] WHERE Event = N'ALTER_TABLE'
PRINT N'Done!'
When run from SSMS, against AdventureWorks 2012, it gives this output:
Dropping CREATE_TABLE events from DatabaseLog table...
(70 row(s) affected)
Dropping ALTER_TABLE events from DatabaseLog table...
(117 row(s) affected)
Done!
How do I reproduce this in .NET, including the rows affected lines?
By hooking into the InfoMessage event on the SqlConnection, I get the output of the PRINT statements, but this doesn't include the rows affected lines. Ideally, I want to capture the output exactly as if it was run in SSMS.
Note: The script is user-supplied, so modifying the script to output the row counts manually is not an option.
SqlCommandfor each statement and then useExectueNonQuery()method that returns rows affectedExecuteNonQuery()from SMO, in order to handle GO separators. Splitting the script into separateSqlCommands is not an option, because that doesn't work if the script uses variables.