如何登录在T-SQL [英] How to log in T-SQL
问题描述
我使用ADO.NET来访问SQL Server 2005,并希望能够从里面那个我打电话的T-SQL存储过程记录。这个现象的原因可能吗?
I'm using ADO.NET to access SQL Server 2005 and would like to be able to log from inside the T-SQL stored procedures that I'm calling. Is that somehow possible?
我无法使用ADO.NET的时候,看到从print'语句输出,因为我想用日志记录只是debuging理想的解决办法是从Sysinternals的发出邮件的DebugView。
I'm unable to see output from the 'print'-statement when using ADO.NET and since I want to use logging just for debuging the ideal solution would be to emit messages to DebugView from SysInternals.
推荐答案
我通过编写SQLCLR-过程埃里克ž胡子建议解决了这一点。该组件必须有一个强名称密钥文件进行签名。
I solved this by writing a SQLCLR-procedure as Eric Z Beard suggested. The assembly must be signed with a strong name key file.
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;
}
}
}
创建一个密钥和一个登录:
Created a key and a login:
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的:
Imported it into 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
然后我就能够使用登录T-SQL程序
Then I was able to log in T-SQL procedures using
exec Debug @message = 'Hello World'
这篇关于如何登录在T-SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!