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);
}
}
}
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
reconfigure
go
3.SQL Server中注册DLL
将SQLServerCLRTest.dll拷贝到服务器中
use testDB
create assembly asmHelloWorld from 'c:\SQLServerCLRTest.dll'
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
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')
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

浙公网安备 33010602011771号