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?
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With