SQL里执行CLR c#代码

时间:2023-03-09 08:29:26
SQL里执行CLR c#代码

这里只说一个重点:

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

SQL里执行CLR c#代码

执行以下代码才会起作用

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