Search code examples
c#sql-serverdata-access-layerssms

C# Handle on SQL Server Message Output


When executing scripts in SQL Server Management Studio, messages are often generated that display in the message window. For example when running a backup of a database:

10 percent processed.

20 percent processed.

Etc...

Processed 1722608 pages for database 'Sample', file 'Sampe' on file 1.

100 percent processed.

Processed 1 pages for database 'Sample', file 'Sample_Log' on file 1.

BACKUP DATABASE successfully processed 1722609 pages in 202.985 seconds (66.299 MB/sec).

I would like to be able to display these message in a C# application that is running SQL scripts against a database. However, I cannot figure out how to get a handle on the message output from SQL as it is generated. Does anybody know how to do this? It doesn't matter to me which connection framework I have to use. I'm relatively comfortable with LINQ, NHibernate, Entity Framework, ADO.Net, Enterprise Library, and am happy to learn new ones.


Solution

  • The SqlConnection.InfoMessage event occurs when SQL Servers returns a warning or informational message. This website shows a possible implementation.