1. 程式人生 > 資料庫 >Sql Server 資料庫中呼叫dll檔案的過程

Sql Server 資料庫中呼叫dll檔案的過程

1.首先新建一個空的解決方案,並新增一個類庫,程式碼如下,編譯並生產dll

using System; 
using System.Collections.Generic; 
using System.Data.SqlTypes; 
using System.Linq; 
using System.Text; 
namespace TEST 
{ 
  public class TestTrans 
  { 
    [Microsoft.SqlServer.Server.SqlFunction] 
    public static SqlString GenerateDecryptString(string name) 
    { 
      string decode = string.Empty; 
      decode = string.Format("HELLO WORLD {0}!",name);//DecryptString(dataXML.Value); 
      SqlString sqlValue = new SqlString(decode); 
      return sqlValue; 
    } 
  } 
} 

2.啟用CLR功能

預設情況下,SQL Server中的CLR是關閉的,所以我們需要執行如下命令開啟CLR:

 exec sp_configure 'clr enabled',1  
 reconfigure  
 Go

3.將程式集引用到資料庫中

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

4.建立函式

CREATE FUNCTION dbo.clrHelloWorld   
(   
  @name as nvarchar(200)   
)   
RETURNS nvarchar(200)  
 AS EXTERNAL NAME testHelloWorld.[TEST.TestTrans].GenerateDecryptString  

5.呼叫函式

SELECT dbo.clrHelloWorld('耿耿') 

6.執行結果

HELLO WORLD 耿耿!

總結

以上所述是小編給大家介紹的Sql Server 資料庫中呼叫dll檔案的過程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!