Snowfun

导航

 

1.VS2005中创建一个项目SQLServerCLRTest,创建一个类CLRFunctions.cs 

View Code
using System;
using System.Collections.Generic;
using System.Text;

namespace SQLServerCLRTest
{
    public class CLRFunctions
    {
        //为了让SQL Server可以调用它,它必须是public 和static的
        public static string HelloWorld(string Name)
        {
            return ("Hello" + Name);
        }
    }
}

 

2.启用SQL Server中的CLR

exec sp_configure 'clr enabled',1 ;
reconfigure 
go

 

3.SQL Server中注册DLL

SQLServerCLRTest.dll拷贝到服务器中

use testDB
create assembly asmHelloWorld from 'c:\SQLServerCLRTest.dll'

 

4.在SQL Server中调用.net方法,在函数内使用"EXTERNAL NAME"来通知SQL Server使用CLR功能

use testDB
create  function [dbo].[clrHelloWorld](@name nvarchar(200) )    --等于.net里面的string类型  
RETURNS nvarchar(200)      
with returns null on null input --只要调用函数的时候任何一个参数为NULL,函数返回值将会是NULL
as 
external NAME            --使用external NAME调用.net方法
asmHelloWorld.[SQLServerCLRTest.CLRFunctions].HelloWorld

 

5.调用.net方法

use testDB
select dbo.clrHelloWorld('Markss')

 

 

 

 http://www.cnblogs.com/doc/archive/2009/02/11/1388513.html

http://www.cnblogs.com/wshcn/archive/2011/12/02/2271630.html

http://www.cnblogs.com/liminzhang/archive/2007/01/12/618324.html

posted on 2012-09-10 17:17  Snowfun  阅读(867)  评论(0)    收藏  举报