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);
4
  • You don't need to use N. Commented Sep 21, 2013 at 11:34
  • Hpw do it ? i add non english string Commented Sep 21, 2013 at 11:37
  • Do you have tried without N? Commented Sep 21, 2013 at 11:39
  • 2
    What datatype are your columns in the News table? Commented Sep 21, 2013 at 13:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.