sql-server – 如何登录T-SQL

前端之家收集整理的这篇文章主要介绍了sql-server – 如何登录T-SQL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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

然后我可以使用T-sql程序登录

exec Debug @message = 'Hello World'
原文链接:https://www.f2er.com/mssql/81634.html

猜你在找的MsSQL相关文章