C# code:
using (SqlCommand myCommand = new SqlCommand("My_StoredProcedure", myConnection))
{
myCommand.Parameters.AddWithValue("@Parameter","Quản trị");
}
Stored procedure:
ALTER PROCEDURE [dbo].[My_StoredProcedure]
(@Parameter NVARCHAR(MAX))
Things I tried:
Changed
AddWithValue
in order to setSqlDbType.VarChar
as belowmyCommand.Parameters.AddWithValue("@Parameter", SqlDbType.VarChar).Value = "Quản trị";
Then changed stored procedure parameter type to
varchar
as belowALTER PROCEDURE [dbo].[My_StoredProcedure] (@Parameter VARCHAR(MAX))
Question:
Things I tried did not work to shown correct text value. Text always appears as Qu?n tr?
in the stored procedure side.
If I try below this works but this is not what I want. I want to add "N" prefix from code (C#) side. So I'm not sure how to achieve this.
ALTER PROCEDURE [dbo].[My_StoredProcedure]
(@Parameter NVARCHAR(MAX) = N'Quản trị')
What kind of solution should I apply in C# or SQL Server?
Any help will be appreciated.
Thank you
nvarchar
andAddWithValue
is correct for Unicode strings (varchar
is not correct). What does your SP do with the value? Can you provide a minimal reproducible example that reproduces this issue with SSMS?cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)
nvarchar
, notvarchar
. And you shouldn't need to do anything else. TheN
prefix is a T-SQL language construct relating to string literals. If you're not writing a string literal in T-SQL, that language's conventions are irrelevant.N
prefix fornvarchar
strings is a proper way to use Unicode characters, but you should useSqlDbType.NVarChar
to pass those kind of strings from C# to SQL Server parameters.