Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store √ and ✓ (tick mark) symbols

I am trying to store and symbols in an nvarchar(50) column.

Here is code sample

vtick.Text = "√";
vvtick.Text = "✓";

INSERT INTO qstnscon (vt,pt) VALUES ('"+vtick.Text+"','"+vvtick.Text+"')";

But instead it stores simple characters (e.g. v). What is causing this problem?

like image 571
SD7 Avatar asked Dec 11 '13 13:12

SD7


Video Answer


1 Answers

First of all - those columns vt and pt must be NVARCHAR datatype - otherwise you cannot store those special symbols.

Secondly: when storing Unicode characters, you MUST prefix them with a N'...' :

INSERT INTO qstnscon (vt, pt) VALUES (N'"+vtick.Text+"', N'"+vvtick.Text+"')";
                                      ^                  ^
-- these are needed!  ----------------+------------------+

Use this technique (prefix with N) if you're inserting data from SQL Server Management Studio.

From your C# code: use a parametrized query!

INSERT INTO qstnscon (vt, pt) VALUES (@vt, @pt);

and then define them as SqlParameter of type SqlDbType.NVarChar from C#

string insertStmt = "INSERT INTO qstnscon (vt, pt) VALUES (@vt, @pt);";

using(SqlCommand cmd = new SqlCommand(insertStmt, yourDbConnection))
{
    cmd.Parameters.Add("@vt", SqlDbType.NVarChar, 50).Value = ".....";
    cmd.Parameters.Add("@pt", SqlDbType.NVarChar, 50).Value = ".....";

    yourDbConnection.Open();
    cmd.ExecuteNonQuery();
    yourDbConnection.Close();
}
like image 194
marc_s Avatar answered Oct 16 '22 09:10

marc_s