我正在使用ADO.NET访问sql Server 2005,并希望能够从我正在调用的T-sql存储过程中进行日志记录.这是可能吗?
在使用ADO.NET时,我无法看到’print’语句的输出,因为我想使用日志记录来进行调试,理想的解决方案是从SysInternals发送DebugView的消息.
解决方法
我通过编写一个sqlCLR程序来解决这个问题,Eric Z Beard建议.程序集必须使用强名称密钥文件进行签名.
using System; using System.Data; using System.Data.sqlClient; using System.Data.sqlTypes; using Microsoft.sqlServer.Server; public partial class StoredProcedures { [Microsoft.sqlServer.Server.sqlProcedure] public static int Debug(string s) { System.Diagnostics.Debug.WriteLine(s); return 0; } } }
创建密钥和登录名:
USE [master] CREATE ASYMMETRIC KEY DebugProcKey FROM EXECUTABLE FILE = 'C:\..\sqlServerProject1\bin\Debug\sqlServerProject1.dll' CREATE LOGIN DebugProcLogin FROM ASYMMETRIC KEY DebugProcKey GRANT UNSAFE ASSEMBLY TO DebugProcLogin
将其导入到sql Server中:
USE [mydb] CREATE ASSEMBLY sqlServerProject1 FROM 'C:\..\sqlServerProject1\bin\Debug\sqlServerProject1.dll' WITH PERMISSION_SET = unsafe CREATE FUNCTION dbo.Debug( @message as nvarchar(200) ) RETURNS int AS EXTERNAL NAME sqlServerProject1.[StoredProcedures].Debug
exec Debug @message = 'Hello World'