问题
I have a SQLCommand :
"Update Customers Set Name = @name where code = @code"
and this code:
cmd.Parameters[0].Value = "بهروز";//(some Unicode characters)
cmd.Parameters[1].Value = 1;
cmd.ExecuteNonQuery();
or this code:
UpdateCommand.CommandText = "UPDATE [Customers] SET [Name] = @p1 WHERE (([Code] = @p2) AND ((@p3 = 1 AND [Name] IS NULL) OR ([Name] = @p4)))";
UpdateCommand.Parameters.Add("@p1", System.Data.SqlDbType.SmallInt, 0, "Name");
UpdateCommand.Parameters.Add("@p2", System.Data.SqlDbType.NVarChar, 0, "Code");
UpdateCommand.Parameters.Add("@p3", System.Data.SqlDbType.NText, 0, "Name");
UpdateCommand.Parameters.Add("@p4", System.Data.SqlDbType.SmallInt, 0, "Name");
and when I Select Customers Table I have lots of "?"s.
why does SQLCommand work Correct when working with a SQLDataAdapter?
How can i convert my Unicode Data to ANSI?
edit:
in other words :
what code does SQLDataAdapter use?
anyone has the source code of that part of .net framework?
回答1:
Dear Behrooz just use a simple SQL command
N'ناصر حاجلو'
whenever you use N you forcesql to use unicode and nothing will corrept.
回答2:
All strings in .NET are Unicode. There is no such thing as an ANSI string within .NET. If you want a string encoded into a byte array as ANSI, use Encoding.GetBytes.
Your issue may be with the way its sending data to the stored procedure. I think you need to add the sql datatype to the parameter. Try the following code:
cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 30);
See the SqlParameterCollection.Add Method for more information.
回答3:
At a guess: try setting the SqlDbType
of the parameter explicitly:
cmd.Parameters[0].Value = "بهروز";//(some Unicode characters)
cmd.Parameters[0].SqlDbType = SqlDbType.NVarChar;
cmd.Parameters[1].Value = 1;
cmd.ExecuteNonQuery();
来源:https://stackoverflow.com/questions/1992688/what-happens-to-unicode-in-a-system-data-sqlcommand