简体   繁体   English

MS SQL Server 2008中的特殊字符插入

[英]Special character insertion in MS SQL Server 2008

Xyz's Goods Hospital @ St. Petersburg Division is stored in a string variable named _value. Xyz的商品医院@圣彼得堡分部存储在名为_value的字符串变量中。 Now what will be the C# sting query to insert this value in a MS SQL table. 现在,将C#字符串查询插入到MS SQL表中的值将是什么。

You could try: 您可以尝试:

using(SqlCommand cmd = new SqlCommand(
    "INSERT INTO your_table (field1) SELECT @val",
    dbconn)) 
{
    cmd.Parameters.Add("@val", your_string);
    try { cmd.ExecuteNonQuery(); }
    catch (Exception ex) { Debug.WriteLine(ex.Message); }
}

EDITED after OP comment: OP评论后编辑:
If you have to insert in multiple columns you can use this different query: 如果必须在多列中插入,则可以使用以下不同查询:

"INSERT INTO your_table (field1, field2, field3) SELECT @val1, @val2, @val3"

and in code add and assign value to every query parameter . 在代码中为每个查询参数添加并赋值。

Assign the value to the going into the database as HttpUtility.HtmlEncode(named _value); 将值作为HttpUtility.HtmlEncode(named_value);分配给要进入数据库的值。

and then while retrieving it use HttpUtility.HtmlDecode(value) . 然后在检索它时使用HttpUtility.HtmlDecode(value)。 Even if you enter it directly it should insert it .. Could please be more specific what exactly do you want to achieve from this 即使您直接输入它,也应该插入它。.请更具体地说明您希望从此实现什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM