Sql Server 數(shù)據(jù)庫中調(diào)用dll文件的過程

2018-12-27 14:20:10 來源:互聯(lián)網(wǎng)作者:Andrewniu 人氣: 次閱讀 486 條評論

文章主要介紹了Sql Server 數(shù)據(jù)庫中調(diào)用dll文件的過程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨小編一起學(xué)習(xí)吧...

文章主要介紹了Sql Server 數(shù)據(jù)庫中調(diào)用dll文件的過程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨小編一起學(xué)習(xí)吧

1.首先新建一個(gè)空的解決方案,并添加一個(gè)類庫,代碼如下,編譯并生產(chǎn)dll

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlTypes;
  4. using System.Linq;
  5. using System.Text;
  6. namespace TEST
  7. {
  8.   public class TestTrans
  9.   {
  10.     [Microsoft.SqlServer.Server.SqlFunction]
  11.     public static SqlString GenerateDecryptString(string name)
  12.     {
  13.       string decode = string.Empty;
  14.       decode = string.Format("HELLO WORLD {0}!", name);//DecryptString(dataXML.Value);
  15.       SqlString sqlValue = new SqlString(decode);
  16.       return sqlValue;
  17.     }
  18.   }
  19. }

2.啟用CLR功能

默認(rèn)情況下,SQL Server中的CLR是關(guān)閉的,所以我們需要執(zhí)行如下命令打開CLR:

  1. exec sp_configure 'clr enabled',1 
  2. reconfigure 
  3. Go

3.將程序集引用到數(shù)據(jù)庫中

  1. CREATE ASSEMBLY testHelloWorld FROM 'C:\TEST.dll'   --('C:/TEST.dll'w為錯(cuò)誤寫法)

4.創(chuàng)建函數(shù)

  1. CREATE FUNCTION dbo.clrHelloWorld  
  2. (  
  3.   @name as nvarchar(200)  
  4. )  
  5. RETURNS nvarchar(200) 
  6.  AS EXTERNAL NAME testHelloWorld.[TEST.TestTrans].GenerateDecryptString

5.調(diào)用函數(shù)

  1. SELECT dbo.clrHelloWorld('耿耿')

6.執(zhí)行結(jié)果

HELLO WORLD  耿耿!

總結(jié)

以上所述是小編給大家介紹的Sql Server 數(shù)據(jù)庫中調(diào)用dll文件的過程,希望對大家有所幫助