Towhid
Towhid

Reputation: 2084

Special character insertion in MS SQL Server 2008

Xyz's Goods Hospital @ St. Petersburg Division is stored in a string variable named _value. Now what will be the C# sting query to insert this value in a MS SQL table.

Upvotes: 0

Views: 722

Answers (2)

Marco
Marco

Reputation: 57593

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:
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 .

Upvotes: 5

user673081
user673081

Reputation: 9

Assign the value to the going into the database as HttpUtility.HtmlEncode(named _value);

and then while retrieving it use 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

Upvotes: -1

Related Questions