C# 建立數(shù)據(jù)庫 CRL 項(xiàng)目 .Net 3.5 public partial class MyClr{ [Microsoft.SqlServer.Server.SqlFunction] public static SqlBoolean CLRFBitContains(string MyBigIntValue, int Row) { // 在此處放置代碼 return (new MyBigInt(MyBigIntValue) MyBigInt.C
C# 建立數(shù)據(jù)庫 CRL 項(xiàng)目 .Net 3.5
public partial class MyClr { [Microsoft.SqlServer.Server.SqlFunction] public static SqlBoolean CLRFBitContains(string MyBigIntValue, int Row) { // 在此處放置代碼 return (new MyBigInt(MyBigIntValue) & MyBigInt.CreateByBitPositon(Convert.ToUInt32(Row))) == MyBigInt.Zero; } }
數(shù)據(jù)庫初始化:
exec sp_configure 'show advanced options', '1'; go reconfigure; go exec sp_configure 'clr enabled', '1' go reconfigure; exec sp_configure 'show advanced options', '1'; go ALTER DATABASE DB_Name set TRUSTWORTHY on; go
數(shù)據(jù)庫添加程序集:
create assembly MyClr from 'R:\MyApp\MyClr\MyClr\bin\Debug\MyClr.dll' WITH permission_set = Safe;
數(shù)據(jù)庫添加函數(shù)映射
create function ClrFBitContains ( @val as nvarchar(4000) , @RowIndex as int ) returns bit as external name MyClr.MyClr.CLRFBitContains ;
TestSql:
select dbo.ClrFBitContains('F0F',11)
======================================================
原文很丑: http://www.ej38.com/showinfo/sqlserver-140374.html
在sqlserver中如果要使用一個(gè)程序集一般有如下注意事項(xiàng)
一:打開sqlserver 的CLR支持
--在Sql Server中執(zhí)行這段代碼可以開啟CLR
exec sp_configure 'show advanced options', '1';
go
reconfigure;
go
exec sp_configure 'clr enabled', '1'
go
reconfigure;
exec sp_configure 'show advanced options', '1';
go
二:是否需要訪問外部資源,如果需要訪問外部資源還需要執(zhí)行下面的
--打開數(shù)據(jù)庫的TRUSTWORTHY 屬性
ALTER DATABASE DB_Name set TRUSTWORTHY on;
**在sqlserver中如果要使用一個(gè)程序集一般有兩種方法
方法一:通過T-SQL手動(dòng)將該程序集放入到SQL Server 其步驟如下
一:創(chuàng)建將要使用的程序集.dll
程序上沒什么稀奇的,跟普通的ado.net的程序差不多主要就是方法上加了一個(gè)屬性 (
二:部署程序集到sqlserver
a打開sqlserver中clr中相關(guān)的支持(方法如上)
b添加程序集到sqlserver
CREATE assembly SqlServerProject1
authorization dbo
from 'E:\test\dotnet\SqlServerProject1\SqlServerProject1\bin\SqlServerProject1.dll'
with permission_set=external_access
go
c添加存儲(chǔ)過程
--添加存儲(chǔ)過程
CREATE PROCEDURE [dbo].[存儲(chǔ)過程名稱]
@from [nvarchar](50), -- 參數(shù)列表
@to [nvarchar](50)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [程序集的名稱].[StoredProcedures].[存儲(chǔ)過程名稱]
d執(zhí)行存儲(chǔ)過程
exec CLRSPTest @sql='select * from t2',@path='e:\a\11.txt'
方法二:通過Visual Studio 2005創(chuàng)建供并部署SQL Server 2005用的Assembly
一:打開Visual Studio 2005新建項(xiàng)目類型為數(shù)據(jù)庫的sqlserver項(xiàng)目
二:創(chuàng)建所需要的存儲(chǔ)過程,函數(shù)等
三:編譯該項(xiàng)目
四:部署該項(xiàng)目(此時(shí)我們打開sqlserver相應(yīng)的數(shù)據(jù)庫可以看到存儲(chǔ)過程目錄及程序集等目錄下有相應(yīng)創(chuàng)建的對(duì)象了)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com