Unicode SQL Query W / Parameter instead of N Prefix

I have an insert request to execute from C # in a SQL Server database.

The column I am inserting is of type nvarchar.

The data that I insert in this column is not English.

Is it enough for me to use AddWithValue to transfer non-English data to the server? like this example:

string dogName = "Χ’Χ‘Χ¨Χ™Χͺ"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("INSERT INTO Dogs1(Name) VALUES @Name", connection)) { command.Parameters.AddWithValue("Name", dogName); command.ExecuteNonQuery(); } } 

Or should I use the N prefix to declare it unicode? as they say here .

+6
source share
2 answers

If I understand the question correctly, you can explicitly specify the SqlCommand parameter as a specific data type. You can install it as nvarchar, as shown at the following link: http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx

This code snippet is taken directly from MSDN:

 SqlParameter parameter = new SqlParameter(); parameter.ParameterName = "@CategoryName"; parameter.SqlDbType = SqlDbType.NVarChar; parameter.Direction = ParameterDirection.Input; parameter.Value = categoryName; 

This uses an explicitly created SqlParameter instance, but it is the same idea indexing the SqlParameterCollection of the SqlCommand instance.

+3
source

I believe the link below only talks about the values ​​inside SQL itself.

As far as I know, the code that you have should be absolutely right - otherwise there would be no Unicode text pointer.

Of course, it is probably worth checking this out, but I would be very surprised if this did not work.

+4
source

Source: https://habr.com/ru/post/898022/


All Articles