sherifffruitfly said:
1) You can't add a reference to system.win.forms to a SQL Server
Project
2) You can't add a referenceto a non-SQL Server Project to a SQL
Server Project
Is there any other way to get a call to MessageBox from sqlclr code?
Yes, but it won't do you much good.
The fact that the Windows Forms assemblies can't be referenced isn't an
obstacle; SQL CLR asssemblies can (unfortunately) still P/Invoke directly to
Win32 functions, so you can call MessageBox() directly.
The more pressing reason is that SQL Server runs as a service, and services
can't interact with the desktop unless they're flagged as such (and no
system administrator worth his money is going to do that, because it opens
up security holes). You can try this for testing, but SQL Server will need
to be restarted, and having it run as an interactive service is totally
unacceptable for production environments.
Another post mentioned NET SEND. Be aware that Windows XP SP2 has the
Messenger service that's required for this to work disabled by default, and
the firewall is set to block it as well. From Vista and Server 2008, the
service is no longer supported at all. Basically, it's a dead-end
technology. If you can get it to work, great, but don't become reliant on it.
Alternatives to message boxes depend on what you need them for:
- If you want to trace values as your code is executing, write it to
somewhere else instead. You can use the Application event log, or open a
text file and append to that. You could even insert records in a table, as
long as you're running on a database server... You may need to fiddle with
permissions to get these things to work, but they're much better choices
than single-stepping through everything.
- If you want to stop your code and wait for a keypress for testing
purposes, that's... just really not appropriate for SQL Server-hosted code.
If you need to test, do it outside SQL Server -- properly separate your
classes and set up a separate project for testing. If timing is an issue,
you can try a delay -- just not something that requires input.
- If your code needs a user to confirm something (it's not just for
testing), don't host it in SQL Server period. You can run it as an
application or a service on the same machine as SQL Server if you need to be
close to the database, but hosting is not an appropriate solution in this case.