SQL里执行CLR c#代码

本文介绍了在 SQL Server 中使用 CLR (Common Language Runtime) 的一些关键步骤与注意事项,包括启用 CLR 支持、处理 .NET 版本兼容性问题、数据类型映射以及 CLR 程序集的更新方法。

这里只说一个重点:

 

1.直接在sql里执行clr代码的时候,sql还是会报错 说没有启用 clr

 

执行以下代码才会起作用

EXEC sp_configure 'clr enabled', 1;  RECONFIGURE WITH OVERRIDE;


2.sql2008 只能识别.net 3.5的

 

 3.c#里的string 对应 sql里的nvarchar

 

 

4.修改clr,如果修改不成功,则只能删除所有引用再重新创建 

ALTER ASSEMBLY ComplexNumber 
FROM 'C:\ComplexNumber.dll' 

以下为举例说明,清除sql里的html标记
alter FUNCTION [dbo].[ReplaceHtmlTag]  
(  
@html AS NVARCHAR(max), 
@length INT=500 
)  
RETURNS nvarchar(max) 
AS  
EXTERNAL NAME [SqlCLR].[NetSkycn.Data.SqlHelper].[ReplaceHtmlTag];  
GO 

  

/// <summary>
		/// 清除html标记
		/// </summary>
		/// <param name="html"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		[SqlFunction(IsDeterministic = true, DataAccess = DataAccessKind.None)] 
		public static SqlString ReplaceHtmlTag(string html, int length = 0)
		{
			string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
			strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");

			if (length > 0 && strText.Length > length)
				return strText.Substring(0, length);

			return (SqlString)strText;
		}

  

参考链接:

http://zhoufoxcn.blog.51cto.com/792419/859245/

https://support.microsoft.com/en-us/help/2120850/error-message-after-you-restore-a-sql-server-2008-32-bit-dynamics-pos

--修改clr程序集

https://serverfault.com/questions/323014/how-to-update-a-clr-assembly-without-dropping-assembly-from-sql-server/323035

转载于:https://www.cnblogs.com/xinzhyu/p/7338126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值