如何在C#SQL Server 2008中存储特殊字符 [英] How to store special characters in C# SQL Server 2008
问题描述
如何存储特殊字符,例如> < ';在sql server中。
我用的如下:
cmd.Parameters.Add (@ Messess,SqlDbType.VarChar,50).Value = Server.HtmlEncode(txtBIAttenNurse.Text.Trim()。Replace(',''));
例如:我给出一个> b - 它在输出a中显示> b。
请解释
什么数据类型可以使用?
什么类型的查询可以使用?
如何存储?
怎么样检索?
How to store special characters like > < ' " ; in sql server.
I am using like below:
cmd.Parameters.Add("@Message", SqlDbType.VarChar, 50).Value = Server.HtmlEncode(txtBIAttenNurse.Text.Trim().Replace("'", "''"));
For ex: i give a>b - it show in output a>b.
Pls explain
what data type can use?
what type of query can use?
how to store?
how to retrieve?
推荐答案
您可以使用Var-char / text数据类型在sqlDB中存储此类型的字符。
我认为你在使用HtmlDecode()方法从数据库中提取时解码输出。
例如:想要插入如下数据:a> ; b
You can use Var-char/text datatype to store this type of characters in sqlDB.
i think you are decoding the output when fetching from database using HtmlDecode() method.
e.g: Want to insert data like: a>b
cmd.Parameters.Add("@Message", SqlDbType.VarChar, 50).Value = Server.HtmlEncode(txtBIAttenNurse.Text.Trim().Replace("'", "''"));
您的代码适用于数据插入。
但是当您提取数据时,请注意您真正要显示的内容。
如果您想显示为> b然后使用HtmlDecode()否则使用纯文本。不需要解码输出。
见这里。
https://msdn.microsoft.com/en-us/library/hwzhtkke(v=vs.110) .aspx [ ^ ]
https: //msdn.microsoft.com/en-us/library/ms525347(v=vs.90).aspx [ ^ ]
http://www.dotnetperls.com / htmlencode-htmldecode [ ^ ]
your code is fine for data insertion.
but when you are fetching the data then be careful what you really want to showing.
if you want to show like a>b then use HtmlDecode() otherwise use plain text.no need to decode the output.
see here.
https://msdn.microsoft.com/en-us/library/hwzhtkke(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/ms525347(v=vs.90).aspx[^]
http://www.dotnetperls.com/htmlencode-htmldecode[^]
这篇关于如何在C#SQL Server 2008中存储特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!