惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
SegmentFault 最新的问题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Attack and Defense Labs
Attack and Defense Labs
F
Full Disclosure
Vercel News
Vercel News
N
News | PayPal Newsroom
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
H
Heimdal Security Blog
P
Privacy International News Feed
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
L
Lohrmann on Cybersecurity
D
Docker
Recent Announcements
Recent Announcements
Security Archives - TechRepublic
Security Archives - TechRepublic
人人都是产品经理
人人都是产品经理
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 叶小钗
Google Online Security Blog
Google Online Security Blog
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
S
Secure Thoughts
博客园 - Franky
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Latest news
Latest news
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学

博客园 - yo

Silverlight 缺陷 - yo - 博客园 SilverLight Controls WF 4.0 中 Persistence 异常 - yo SilverLight框架初探-View - yo - 博客园 SilverLight框架初探-RiaService SilverLight框架初探-ViewModel SilverLight框架初探 与客户“调情” AG_E_PARSER_BAD_PROPERTY_VALUE 定义属于自己的Routing 数据契约的序列化 WCF客户端配置问题 关闭EXCEL进程 母版页中控件ID获取 - yo - 博客园 Reportviewer - Error: ASP.NET session has expired - yo C# DLL动态调用 Sharepoint List faults - yo 出错页面webpar的t删除 quickpart
SQL CLR
yo · 2009-04-16 · via 博客园 - yo

一、配置 SQL Server,使之允许 CLR 集成:

  1.单击“开始”按钮,依次指向“所有程序”、Microsoft SQL Server 2005 和“配置工具”,然后单击“外围应用配置器”。

  2.在 SQL Server 2005 外围应用配置器工具中,单击“功能的外围应用配置器”。

  3.选择您的服务器实例,展开“数据库引擎”选项,然后单击“CLR 集成”。

  4.选择“启用 CLR 集成”。

     此外,您可以在 SQL Server 中运行以下查询(此查询需要 ALTER SETTINGS 权限):

   USE [database]
   sp_configure 'clr enabled', 1;
   GO
   RECONFIGURE;
   GO

二、创建 SQL Server Project,配置数据库连接信息。右键打开该项目属性,选择“database ”,将Permission Level设为“unsafe”

三、

项目创建后添加一个.cs 文件,如下演示如何进行表值函数扩展

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Diagnostics;
using System.Data.SqlTypes;
using System.Collections;

public partial class FuncTest
{
    //表值函数定义.第一个方法 (InitMethod) 赋予 SqlFunction 属性,用于将它指定为该表值函数的入口点
    //此方法必须返回 IEnumerable 或 IEnumerator 对象。该对象包含将用于填充返回表的数据。
    //执行该函数时,SQL Server 将循环访问 IEnumerator 对象中的每个对象,并使用它来填充数据行。
    //为此,它要将该对象传递到该类中的第二个方法 FillRow。此方法会将该对象转换成返回表中的某一行。
    //此方法在 SqlFunction 属性的 FillRowMethodName 参数中指定
    //部署项目后在数据库中会生成相应的表值函数dbo.ReadEventLog
    [SqlFunction(TableDefinition = "logTime datetime,Message nvarchar(4000),Category nvarchar(4000),InstanceId bigint",
                Name = "ReadEventLog", FillRowMethodName = "FillRow")]
    public static IEnumerable InitMethod(String logname)
    {
        return new EventLog(logname, Environment.MachineName).Entries;
    }
    public static void FillRow(Object obj, out SqlDateTime timeWritten, out SqlChars message, out SqlChars category, out long instanceId)
    {
        EventLogEntry eventLogEntry = (EventLogEntry)obj;
        timeWritten = new SqlDateTime(eventLogEntry.TimeWritten);
        message = new SqlChars(eventLogEntry.Message);
        category = new SqlChars(eventLogEntry.Category);
        instanceId = eventLogEntry.InstanceId;
    }
}

项目创建后添加一个Stored Procedure 文件,如下演示如何进行Stored Procedure扩展

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]
    /// <summary>
    ///
    /// </summary>
    /// <param name="greeting">return value</param>
    public static void SayHello(ref string greeting)
    {
        SqlMetaData columnInfo = new SqlMetaData("ColumnName", SqlDbType.NVarChar, 12);
        SqlDataRecord greetingRecord = new SqlDataRecord(new SqlMetaData[] { columnInfo });
        greetingRecord.SetString(0, "HelloWorld");
        // 呼叫 Pipe 对象的 Send 方法将SqlDataRecord 对象直接传送给客户端
        SqlContext.Pipe.Send(greetingRecord);
        //输出参数
        greeting = "Nice to see you!";
    }
 }

四、编译生成dll文件,并部署到数据库中。(Build---->Deploy projectname)

这样就可以将project部署到数据库中对应的程序集,函数,存储过程了

SQL Script生成程序集,函数如下

CREATE ASSEMBLY HelloWorld
from 'd:\HelloWorld.dll'
WITH PERMISSION_SET = unsafe
Go

CREATE FUNCTION ReadEventLog(@logname nvarchar(100))
RETURNS TABLE
(
logTime datetime,
[Message] nvarchar(4000),
Category nvarchar(4000),
InstanceId bigint
)
AS
EXTERNAL NAME HelloWorld.FuncTest.InitMethod
GO
 

CREATE PROCEDURE [dbo].[Hello]
 @greeting [nvarchar](4000) OUTPUT
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SSP].[StoredProcedures].[Hello]
GO
EXEC sys.sp_addextendedproperty @name=N'AutoDeployed', @value=N'yes' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'PROCEDURE', @level1name=N'Hello'

GO
EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFile', @value=N'Hello.cs' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'PROCEDURE', @level1name=N'Hello'

GO
EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFileLine', @value=10 ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'PROCEDURE', @level1name=N'Hello'