How to add “N” prefix to Stored Procedure parameter from c# code [duplicate]












2
















This question already has an answer here:




  • nVarchar and SqlParameter

    5 answers




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 set SqlDbType.VarChar as below



    myCommand.Parameters.AddWithValue("@Parameter", SqlDbType.VarChar).Value = "Quản trị";



  • Then changed stored procedure parameter type to varchar as below



    ALTER 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










share|improve this question















marked as duplicate by Damien_The_Unbeliever, CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Your code looks fine, nvarchar and AddWithValue is correct for Unicode strings (varchar is not correct). What does your SP do with the value? Can you provide a Minimal, Complete, and Verifiable example that reproduces this issue with SSMS?

    – Heinzi
    Nov 23 '18 at 8:14








  • 1





    Did you check this? social.msdn.microsoft.com/Forums/sqlserver/en-US/… Example- cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)

    – Souvik Ghosh
    Nov 23 '18 at 8:18













  • You should be passing it from C# as nvarchar, not varchar. And you shouldn't need to do anything else. The N 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.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:19











  • The N prefix for nvarchar strings is a proper way to use Unicode characters, but you should use SqlDbType.NVarChar to pass those kind of strings from C# to SQL Server parameters.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:20






  • 1





    The second parameter for AddWithValue is always a value not a type. The syntax you showed in your example is for Add.

    – Steve
    Nov 23 '18 at 8:22
















2
















This question already has an answer here:




  • nVarchar and SqlParameter

    5 answers




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 set SqlDbType.VarChar as below



    myCommand.Parameters.AddWithValue("@Parameter", SqlDbType.VarChar).Value = "Quản trị";



  • Then changed stored procedure parameter type to varchar as below



    ALTER 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










share|improve this question















marked as duplicate by Damien_The_Unbeliever, CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Your code looks fine, nvarchar and AddWithValue is correct for Unicode strings (varchar is not correct). What does your SP do with the value? Can you provide a Minimal, Complete, and Verifiable example that reproduces this issue with SSMS?

    – Heinzi
    Nov 23 '18 at 8:14








  • 1





    Did you check this? social.msdn.microsoft.com/Forums/sqlserver/en-US/… Example- cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)

    – Souvik Ghosh
    Nov 23 '18 at 8:18













  • You should be passing it from C# as nvarchar, not varchar. And you shouldn't need to do anything else. The N 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.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:19











  • The N prefix for nvarchar strings is a proper way to use Unicode characters, but you should use SqlDbType.NVarChar to pass those kind of strings from C# to SQL Server parameters.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:20






  • 1





    The second parameter for AddWithValue is always a value not a type. The syntax you showed in your example is for Add.

    – Steve
    Nov 23 '18 at 8:22














2












2








2









This question already has an answer here:




  • nVarchar and SqlParameter

    5 answers




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 set SqlDbType.VarChar as below



    myCommand.Parameters.AddWithValue("@Parameter", SqlDbType.VarChar).Value = "Quản trị";



  • Then changed stored procedure parameter type to varchar as below



    ALTER 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










share|improve this question

















This question already has an answer here:




  • nVarchar and SqlParameter

    5 answers




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 set SqlDbType.VarChar as below



    myCommand.Parameters.AddWithValue("@Parameter", SqlDbType.VarChar).Value = "Quản trị";



  • Then changed stored procedure parameter type to varchar as below



    ALTER 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





This question already has an answer here:




  • nVarchar and SqlParameter

    5 answers








c# sql-server stored-procedures






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:06









marc_s

576k12911111258




576k12911111258










asked Nov 23 '18 at 8:11









Soner SevincSoner Sevinc

6292829




6292829




marked as duplicate by Damien_The_Unbeliever, CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Damien_The_Unbeliever, CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Your code looks fine, nvarchar and AddWithValue is correct for Unicode strings (varchar is not correct). What does your SP do with the value? Can you provide a Minimal, Complete, and Verifiable example that reproduces this issue with SSMS?

    – Heinzi
    Nov 23 '18 at 8:14








  • 1





    Did you check this? social.msdn.microsoft.com/Forums/sqlserver/en-US/… Example- cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)

    – Souvik Ghosh
    Nov 23 '18 at 8:18













  • You should be passing it from C# as nvarchar, not varchar. And you shouldn't need to do anything else. The N 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.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:19











  • The N prefix for nvarchar strings is a proper way to use Unicode characters, but you should use SqlDbType.NVarChar to pass those kind of strings from C# to SQL Server parameters.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:20






  • 1





    The second parameter for AddWithValue is always a value not a type. The syntax you showed in your example is for Add.

    – Steve
    Nov 23 '18 at 8:22



















  • Your code looks fine, nvarchar and AddWithValue is correct for Unicode strings (varchar is not correct). What does your SP do with the value? Can you provide a Minimal, Complete, and Verifiable example that reproduces this issue with SSMS?

    – Heinzi
    Nov 23 '18 at 8:14








  • 1





    Did you check this? social.msdn.microsoft.com/Forums/sqlserver/en-US/… Example- cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)

    – Souvik Ghosh
    Nov 23 '18 at 8:18













  • You should be passing it from C# as nvarchar, not varchar. And you shouldn't need to do anything else. The N 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.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:19











  • The N prefix for nvarchar strings is a proper way to use Unicode characters, but you should use SqlDbType.NVarChar to pass those kind of strings from C# to SQL Server parameters.

    – Tetsuya Yamamoto
    Nov 23 '18 at 8:20






  • 1





    The second parameter for AddWithValue is always a value not a type. The syntax you showed in your example is for Add.

    – Steve
    Nov 23 '18 at 8:22

















Your code looks fine, nvarchar and AddWithValue is correct for Unicode strings (varchar is not correct). What does your SP do with the value? Can you provide a Minimal, Complete, and Verifiable example that reproduces this issue with SSMS?

– Heinzi
Nov 23 '18 at 8:14







Your code looks fine, nvarchar and AddWithValue is correct for Unicode strings (varchar is not correct). What does your SP do with the value? Can you provide a Minimal, Complete, and Verifiable example that reproduces this issue with SSMS?

– Heinzi
Nov 23 '18 at 8:14






1




1





Did you check this? social.msdn.microsoft.com/Forums/sqlserver/en-US/… Example- cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)

– Souvik Ghosh
Nov 23 '18 at 8:18







Did you check this? social.msdn.microsoft.com/Forums/sqlserver/en-US/… Example- cmd.Parameters.Add("@Name", SqlDbType.NVarChar, Name.Length, Name)

– Souvik Ghosh
Nov 23 '18 at 8:18















You should be passing it from C# as nvarchar, not varchar. And you shouldn't need to do anything else. The N 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.

– Damien_The_Unbeliever
Nov 23 '18 at 8:19





You should be passing it from C# as nvarchar, not varchar. And you shouldn't need to do anything else. The N 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.

– Damien_The_Unbeliever
Nov 23 '18 at 8:19













The N prefix for nvarchar strings is a proper way to use Unicode characters, but you should use SqlDbType.NVarChar to pass those kind of strings from C# to SQL Server parameters.

– Tetsuya Yamamoto
Nov 23 '18 at 8:20





The N prefix for nvarchar strings is a proper way to use Unicode characters, but you should use SqlDbType.NVarChar to pass those kind of strings from C# to SQL Server parameters.

– Tetsuya Yamamoto
Nov 23 '18 at 8:20




1




1





The second parameter for AddWithValue is always a value not a type. The syntax you showed in your example is for Add.

– Steve
Nov 23 '18 at 8:22





The second parameter for AddWithValue is always a value not a type. The syntax you showed in your example is for Add.

– Steve
Nov 23 '18 at 8:22












1 Answer
1






active

oldest

votes


















2














I would keep the data type in SQL as an NVARCHAR because that is what it should be and the C# code should like this:



myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value


You have passed in VarChar instead of NVarChar so it makes sense for Unicode not to display.






share|improve this answer





















  • 1





    Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:30











  • You're absolutely correct. Flew past me. Corrected. Thanks.

    – NikosV
    Nov 23 '18 at 8:37













  • I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

    – Soner Sevinc
    Nov 23 '18 at 8:52











  • Where is "Quản trị" coming from? A TextBox?

    – NikosV
    Nov 23 '18 at 9:08











  • @SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

    – Damien_The_Unbeliever
    Nov 23 '18 at 9:56


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I would keep the data type in SQL as an NVARCHAR because that is what it should be and the C# code should like this:



myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value


You have passed in VarChar instead of NVarChar so it makes sense for Unicode not to display.






share|improve this answer





















  • 1





    Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:30











  • You're absolutely correct. Flew past me. Corrected. Thanks.

    – NikosV
    Nov 23 '18 at 8:37













  • I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

    – Soner Sevinc
    Nov 23 '18 at 8:52











  • Where is "Quản trị" coming from? A TextBox?

    – NikosV
    Nov 23 '18 at 9:08











  • @SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

    – Damien_The_Unbeliever
    Nov 23 '18 at 9:56
















2














I would keep the data type in SQL as an NVARCHAR because that is what it should be and the C# code should like this:



myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value


You have passed in VarChar instead of NVarChar so it makes sense for Unicode not to display.






share|improve this answer





















  • 1





    Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:30











  • You're absolutely correct. Flew past me. Corrected. Thanks.

    – NikosV
    Nov 23 '18 at 8:37













  • I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

    – Soner Sevinc
    Nov 23 '18 at 8:52











  • Where is "Quản trị" coming from? A TextBox?

    – NikosV
    Nov 23 '18 at 9:08











  • @SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

    – Damien_The_Unbeliever
    Nov 23 '18 at 9:56














2












2








2







I would keep the data type in SQL as an NVARCHAR because that is what it should be and the C# code should like this:



myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value


You have passed in VarChar instead of NVarChar so it makes sense for Unicode not to display.






share|improve this answer















I would keep the data type in SQL as an NVARCHAR because that is what it should be and the C# code should like this:



myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value


You have passed in VarChar instead of NVarChar so it makes sense for Unicode not to display.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 8:37

























answered Nov 23 '18 at 8:28









NikosVNikosV

10210




10210








  • 1





    Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:30











  • You're absolutely correct. Flew past me. Corrected. Thanks.

    – NikosV
    Nov 23 '18 at 8:37













  • I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

    – Soner Sevinc
    Nov 23 '18 at 8:52











  • Where is "Quản trị" coming from? A TextBox?

    – NikosV
    Nov 23 '18 at 9:08











  • @SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

    – Damien_The_Unbeliever
    Nov 23 '18 at 9:56














  • 1





    Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

    – Damien_The_Unbeliever
    Nov 23 '18 at 8:30











  • You're absolutely correct. Flew past me. Corrected. Thanks.

    – NikosV
    Nov 23 '18 at 8:37













  • I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

    – Soner Sevinc
    Nov 23 '18 at 8:52











  • Where is "Quản trị" coming from? A TextBox?

    – NikosV
    Nov 23 '18 at 9:08











  • @SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

    – Damien_The_Unbeliever
    Nov 23 '18 at 9:56








1




1





Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

– Damien_The_Unbeliever
Nov 23 '18 at 8:30





Add, not AddWithValue. At the moment, this boxes the enum value to object, I'm guessing the system will work out the underlying type of the enum and most likely creates the parameter to have a data type of int.

– Damien_The_Unbeliever
Nov 23 '18 at 8:30













You're absolutely correct. Flew past me. Corrected. Thanks.

– NikosV
Nov 23 '18 at 8:37







You're absolutely correct. Flew past me. Corrected. Thanks.

– NikosV
Nov 23 '18 at 8:37















I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

– Soner Sevinc
Nov 23 '18 at 8:52





I tried myCommand.Parameters.Add("@Parameter", SqlDbType.NVarChar).Value = "Quản trị"; and did not work for me.

– Soner Sevinc
Nov 23 '18 at 8:52













Where is "Quản trị" coming from? A TextBox?

– NikosV
Nov 23 '18 at 9:08





Where is "Quản trị" coming from? A TextBox?

– NikosV
Nov 23 '18 at 9:08













@SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

– Damien_The_Unbeliever
Nov 23 '18 at 9:56





@SonerSevinc - then this all looks right from the C# side, time to see if the stored proc is misbehaving.

– Damien_The_Unbeliever
Nov 23 '18 at 9:56





Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen