萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mssql數據庫 >> SQL2005CLR函數擴展-繁簡轉換的實現代碼

SQL2005CLR函數擴展-繁簡轉換的實現代碼

這個方法比較簡單,用Microsoft.VisualBasic命名空間下強大的字符串處理函數就可以了
c#代碼如下,編譯為BigConvertor.dll
--------------------------------------------------------------------------------

復制代碼 代碼如下:
using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction ]
public static SqlString BigToGB(SqlString inString)
{
if (inString.IsNull) return SqlString .Null;
return (Microsoft.VisualBasic.Strings .StrConv(inString.Value, Microsoft.VisualBasic.VbStrConv .SimplifiedChinese, 0));
}
[Microsoft.SqlServer.Server.SqlFunction ]
public static SqlString GBToBig(SqlString inString)
{
if (inString.IsNull) return SqlString .Null;
return (Microsoft.VisualBasic.Strings .StrConv(inString.Value, Microsoft.VisualBasic.VbStrConv .TraditionalChinese, 0));
}
};


--------------------------------------------------------------------------------
部署代碼如下
--------------------------------------------------------------------------------

復制代碼 代碼如下:
CREATE ASSEMBLY BigConvertor FROM 'E:/sqlclrdata/BigConvertor.dll' WITH PERMISSION_SET = UnSAFE;
--
go
CREATE FUNCTION dbo. xfn_BigToGB
(
@value nvarchar ( max )
)
RETURNS nvarchar ( max )
AS EXTERNAL NAME BigConvertor. UserDefinedFunctions. BigToGB
go
CREATE FUNCTION dbo. xfn_GBToBig
(
@value nvarchar ( max )
)
RETURNS nvarchar ( max )
AS EXTERNAL NAME BigConvertor. UserDefinedFunctions. GBToBig

go


--------------------------------------------------------------------------------
測試代碼如下
--------------------------------------------------------------------------------
/* 測試 */
select dbo. xfn_GBToBig( ' 簡體與繁體文的轉換 ' )
-- 簡體與繁體文的轉換
select dbo. xfn_BigToGB( ' 簡體與繁體文的轉換 ' )
-- 簡體與繁體文的轉換

 

copyright © 萬盛學電腦網 all rights reserved