0

Hi have a sql string where want insert data to sql server , how to use N when i add values like @value

SqlCommand cmd = new SqlCommand("Insert into News (News,title) values (@news,@title)", conn);
string news = TextBox1.Text;
string title = TextBox2.Text;

cmd.Parameters.AddWithValue(@"news", news);
cmd.Parameters.AddWithValue(@"title", title);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
R01
  • 321
  • 2
  • 4
  • 15

1 Answers1

3

Code seems fine - and no, you don't need to use the N prefix anywhere in such a scenario (only when you're writing a string literal in e.g. Management Studio).

The only thing that might be something to look into is explicitly specifying the parameter types:

cmd.Parameters.Add("@news", SqlDbType.NVarChar, 100).Value = news;
cmd.Parameters.Add("@title", SqlDbType.NVarChar, 100).Value = title;

but usually, ADO.NET is pretty good at guessing the right datatype, especially since .NET strings are in fact Unicode already.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459