Asp.net

T-SQL 中的 HTML 編碼?

  • March 12, 2009

是否有任何函式可以在 T-SQL 中對 HTML 字元串進行編碼?我有一個舊數據庫,其中包含諸如“<”、“>”等狡猾的字元。我可以編寫一個函式來替換這些字元,但有更好的方法嗎?

我有一個 ASP.Net 應用程序,當它返回一個字元串時,它包含導致錯誤的字元。ASP.Net 應用程序正在從數據庫表中讀取數據。它不會寫入表本身。

我們有一個遺留系統,它使用觸發器和 dbmail 在輸入表格時發送 HTML 編碼的電子郵件,因此我們需要在電子郵件生成中進行編碼。我注意到 Leo 的版本有一個小錯誤,將 & in 編碼 &lt;&gt;我使用這個版本:

CREATE FUNCTION HtmlEncode
(
   @UnEncoded as varchar(500)
)
RETURNS varchar(500)
AS
BEGIN
 DECLARE @Encoded as varchar(500)

 --order is important here. Replace the amp first, then the lt and gt. 
 --otherwise the &lt will become &amp;lt; 
 SELECT @Encoded = 
 Replace(
   Replace(
     Replace(@UnEncoded,'&','&amp;'),
   '&lt;', '&lt;'),
 '&gt;', '&gt;')

 RETURN @Encoded
END
GO

引用自:https://stackoverflow.com/questions/639393