I have done some searching for a simple, clear example of how to capture all messages when running a query against Microsoft SQL Server using the SQL Management Objects (SMO).
I haven't been at all happy with what I've found, so I thought I would post the following example, hopefully as a clear and concise code example
A few notes:
1 - "Messages" is a text TextBox that I am using, as you might guess, to write messages to
2 - Note the "AddressOf" in the AddHandler method call. This registers the method so that messages can be passed to the application and handled.
3- I had to define MigDBServer as
Private MigDBServer As Microsoft.SqlServer.Management.Smo.Server
4 - It is also necessary to add a reference to the ddl that contains the Microsoft SMO extensions.
Add Reference by going to Project-> Add References.
Then choose Assemblies->Extensions, and select Microsoft.SqlServer.Smo
Click Image to view larger Version: