`
wyf
  • 浏览: 436680 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
社区版块
存档分类
最新评论

SQL Server 2005中创建CLR存储过程

    博客分类:
  • SQL
阅读更多
 

 一、打开vs2005 建立数据库项目。

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void P_GetAllPY(SqlString hzString, out SqlString pyString)
    {
        // 匹配中文字符   
        Regex regex = new Regex("^[\u4e00-\u9fa5]$");
        byte[] array = new byte[2];
        int chrAsc = 0;
        pyString = "";
        int i1 = 0;
        int i2 = 0;
        SqlChars noWChar = (SqlChars)hzString;
        for (int j = 0; j < noWChar.Length; j++)
        {
            // 中文字符   
            if (regex.IsMatch(noWChar[j].ToString()))
            {
                array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
                i1 = (short)(array[0]);
                i2 = (short)(array[1]);
                chrAsc = i1 * 256 + i2 - 65536;
                if (chrAsc > 0 && chrAsc < 160)
                {
                    pyString += noWChar[j].ToString();
                }
                else
                {
                    // 修正部分文字   
                    if (chrAsc == -9254)  // 修正“圳”字   
                        pyString += "Zhen";
                    else
                    {
                        for (int i = (pyValue.Length - 1); i >= 0; i--)
                        {
                            if (pyValue[i] <= chrAsc)
                            {
                                pyString += pyName[i];
                                break;
                            }
                        }
                    }
                }
            }
            // 非中文字符   
            else
            {
                pyString += noWChar[j].ToString();
            }
        }
    }
    #region 拼音编码
    private readonly static int[] pyValue = new int[]   
{   
-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,   
-20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,   
-19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515,   
-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249,   
-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,   
-19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,   
-18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448,   
-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012,   
-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,   
-17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,   
-17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733,   
-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448,   
-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,   
-16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,   
-15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659,   
-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394,   
-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,   
-15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,   
-14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902,   
-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654,   
-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,   
-14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,   
-14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907,   
-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601,   
-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,   
-13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,   
-13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831,   
-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300,   
-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,   
-11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,   
-11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832,   
-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328,   
-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254   
};
    private readonly static string[] pyName = new string[]   
{   
"A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben",   
"Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can",   
"Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng",   
"Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong",   
"Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De",   
"Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui",   
"Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo",   
"Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong",   
"Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han",   
"Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan",   
"Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing",   
"Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke",   
"Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo",   
"La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang",   
"Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun",   
"Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian",   
"Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang",   
"Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning",   
"Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan",   
"Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po",   
"Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu",   
"Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou",   
"Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen",   
"Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu",   
"Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan",   
"Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian",   
"Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai",   
"Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao",   
"Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang",   
"Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun",   
"Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan",   
"Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan",   
"Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo"  
};
    #endregion 拼音编码
};

 

以上获得 汉字的全拼

二、部署(配置数据库连接)

或者在查询分析器中执行注册语句

 

--注册程序集
CREATE ASSEMBLY Proc_GetAllPY from 'E:\程序集存储过程\Proc_GetAllPY\Proc_GetAllPY\bin\Debug\Proc_GetAllPY.dll' WITH PERMISSION_SET = SAFE
GO
--创建存储过程
CREATE PROCEDURE Proc_GetAllPY(@hz nvarchar(1000),@py nvarchar(1000) out)
AS 
EXTERNAL NAME Proc_GetAllPY.StoredProcedures.P_GetAllPY
GO

--开启clr支持
EXEC sp_configure 'clr enabled', 1 
GO 
RECONFIGURE 
GO 

--执行存储过程
declare @str nvarchar(1000)
exec dbo.Proc_GetAllPY '王燕飞' ,@str out
select @str
--关闭clr支持
EXEC sp_configure 'clr enabled', 0 
GO 
RECONFIGURE 
GO

 

分享到:
评论

相关推荐

    在SQL Server 2005中创建CLR存储过程的详细介绍

    首先,让我们深入了解一下如何在SQL Server 2005中创建CLR存储过程。创建过程分为几个步骤: 1. 准备环境: - 确保你有一个SQL Server 2005实例,并且启用了CLR集成。默认情况下,SQL Server 2005安装时会启用CLR...

    使用 SQL Server 2005中的 CLR 集成

    SQL Server 2005中的CLR(Common Language Runtime)集成是一项强大的特性,它允许开发人员使用.NET Framework(如C#、Visual Basic .NET或Visual C++)编写的代码与SQL Server进行深度集成。这一功能扩展了数据库的...

    01.SQL Server 2005中的CLR_Demo

    CLR集成是SQL Server 2005中的一项重要创新,它将.NET Framework引入到数据库引擎内部,使得数据库开发者可以利用.NET的强大功能。通过CLR,我们可以创建更高效、更安全、更易维护的数据库对象,这些对象可以直接在...

    SQL Server 2005中的CLR

    SQL Server 2005中的CLR(Common Language Runtime)是一个重要的功能,它允许开发者使用.NET Framework的编程语言,如C#、Visual Basic .NET或F#,来编写存储过程、触发器、用户自定义函数(UDFs)、类型和聚合等...

    Professional SQL Server 2005 CLR Programming

    #### 二、使用C#编写SQL Server 2005中的存储过程 - **存储过程概述**:存储过程是在数据库上创建的一组预编译的SQL语句。它们可以接受输入参数,执行复杂的操作并返回结果。使用C#编写存储过程可以使开发者充分...

    Microsoft System CLR Types for Microsoft SQL Server 2014

    这些类型使得开发者能够使用.NET Framework的语言,如C#或Visual Basic .NET,来创建和操作SQL Server的对象,如存储过程、函数、触发器等。CLR集成是SQL Server的一个特性,允许将.NET代码嵌入到数据库中,从而扩展...

    Microsoft sql server system clr types 2012

    在SQL Server中,CLR集成允许开发者使用.NET Framework的语言(如C#或VB.NET)编写存储过程、触发器和其他数据库对象,从而提升性能和功能。 描述中提到的“部署查看Windows SQL服务器报表”是指在Windows环境下...

    SQL Server CLR 存储过程实现数据的ZIP压缩和解压

    在SQL Server中创建一个能够处理ZIP压缩和解压缩的CLR存储过程,我们需要遵循以下步骤: 1. **开发.NET项目**:使用Visual Studio或其他.NET IDE创建一个新的类库项目。在项目中,我们可以引入System.IO....

    SQL Server 2005中的CLR应用研究.pdf

    使用CLR存储过程,可以实现T-SQL难以完成的任务,例如调用.NET Framework的高级算法库,处理XML数据,或者执行图形和加密操作。这大大提高了SQL Server的可扩展性和灵活性。 总结来说,SQL Server 2005中的CLR集成...

    microsoft system clr types for sql server 2012

    SQL Server的CLR集成允许用户使用.NET编程语言(如C#或VB.NET)编写存储过程、触发器、函数等数据库对象。SQLSysClrTypes库则包含了处理这些对象所必需的类型和接口,例如,它可以处理空间数据类型,这对于地理信息...

    microsoft system clr types for sql server 2012.7z

    标签“systemclrtypes”和“sqlserver2012”进一步明确了该压缩包的内容,System CLR Types是与SQL Server 2012紧密相关的库。System CLR Types在SQL Server中扮演着重要角色,它提供了对SQL Server高级功能的支持,...

    SQLServer2005_BC.msi SQLServer2005及以后版本 向前 兼容 DTS等功能

    在SQL Server 2005中,DTS被SSIS(SQL Server Integration Services)所取代,SSIS是更强大、更灵活的数据集成工具,能够处理复杂的ETL(提取、转换、加载)工作流。尽管如此,为了确保与早期版本的兼容性,SQL ...

    Microsoft System CLR Types For SQL Server 2012.rar

    在SQL Server中,CLR集成允许开发者使用.NET Framework的全部功能来编写存储过程、用户定义的函数、用户定义的类型、触发器和聚合函数。这样做的好处包括: 1. **性能提升**:对于需要大量计算的任务,使用CLR可以...

    C#创建SQL Server存储过程帮助

    接下来,我们需要在Visual Studio 2005中创建一个新的SQL Server项目。选择“文件”-&gt;“新建项目”,在“新建项目”对话框中选择“Visual C#”下的“Database”,然后选择“SQL Server 项目”。根据向导选择或创建一...

    Professional SQL Server 2005 CLR Programming.rar

    CLR存储过程是用.NET语言编写的可执行代码,可以直接在SQL Server中执行,替代传统的Transact-SQL存储过程。它们提供了更好的性能、更高的安全性和更复杂的业务逻辑处理能力。 4. **用户定义函数(UDF)** CLR ...

    CLR创建存储过程.docx

    通过以上步骤,我们可以使用C#在SQL Server 2005中创建存储过程。这种方式不仅提高了代码的可读性和维护性,还利用了.NET框架的强大功能,如类型安全、实时编译等。此外,CLR集成还支持创建其他数据库对象,如用户...

    SQL SERVER CLR 讲解及小例

    SQL Server CLR(Common Language Runtime)是微软SQL Server数据库引擎中的一个功能,允许开发人员使用.NET Framework的任何语言(如C#、VB.NET或F#)编写存储过程、触发器、用户定义函数(UDFs)、类型和其他...

Global site tag (gtag.js) - Google Analytics