简体   繁体   中英

How to insert unicode string into sql server using c#

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM