Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / September 2007

Tip: Looking for answers? Try searching our database.

Pass unicode to stored procedures

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hamilton Colyer-Long - 29 Sep 2007 09:22 GMT
Hi,

I have an **urgent** need to pass chinese characters into an SQL Server
Express table using a stored procedure. The MSDN documentation informs me
that everything will work 'automatically' if I use NVARCHAR parameters but
all I am getting is question marks. I've found old threads where other
people have the same problem but the only answer available is 'it should
just work'. Hopefully someone here (PLEASE) has more idea why the following
would not work (the result for me is that every unicode character is
converted into a question mark instead of having the correct chinese
charater).

PS The same code works fine for Jet (Access).

CREATE TABLE MyTable (iID INT, sName NVARCHAR(50));
GO

CREATE PROCEDURE MyTable_Insert(@iID INT, @sName NVARCHAR(50))
AS INSERT INTO MyTable(iID, sName) VALUES (@iID, @sName);
GO

DECLARE @RC INT
DECLARE @iID INT
DECLARE @sName NVARCHAR(50)

SET @iID = 1
SET @sName = '??'

EXECUTE @RC = MyTable_Insert @iID, @sName
GO

SELECT * FROM MyTable;

------------
Result: 1 ??
------------

NOTE: Prefixing the variable name with an "N" does fix the problem:
 SET @sName = '??'     // this inserts ??
 SET @sName = N'??'      // this inserts ??
However, this solution is not workable because eventually the parameter will
be set by an existing C# application that passes a string. BTW The
'automatic' detection of string variables should make this unecessary right?

This is an urgent problem for me. A fast response would be greatly
appreciated.

Regards,
Hamilton Colyer-Long

Janusys
www.janusys.com.au
Hamilton Colyer-Long - 29 Sep 2007 09:32 GMT
Is it an omen that this newsgroup filtered unicode from my original post?
The following lines that contain question marks should actually have Chinese
characters:
 SET @sName = '??'
 SET @sName = '??'     // this inserts question marks
 SET @sName = N'??'      // this inserts ??

Other instances of question marks in the original post are "as intended":
 Result: 1 ??

Chinese characters: ??????

Regards,
Hamilton Colyer-Long

Janusys
www.janusys.com.au
Hamilton Colyer-Long - 29 Sep 2007 09:49 GMT
I've reworked the code in the original post slightly to better illustrate my
point (and because I'm desperate for help!). If you copy code below the
dotted line into studio manager and replace the 'QQQ' with Chinese
characters then the following results are obtained:
 1 ??? // This is failure
 2 QQQ // This is success
------------------------------------------------

DROP TABLE MyTable;
GO
DROP PROCEDURE MyTable_Insert;
GO

CREATE TABLE MyTable (iID INT, sName NVARCHAR(50));
GO

CREATE PROCEDURE MyTable_Insert(@iID INT, @sName NVARCHAR(50))
AS
INSERT INTO MyTable(iID, sName) VALUES (@iID, @sName);
GO

DECLARE @RC INT
DECLARE @iID INT
DECLARE @sName NVARCHAR(50)

/* THIS INSERTS QUESTION MARKS */
SET @iID = 1
SET @sName = 'QQQ'
EXECUTE @RC = MyTable_Insert @iID, @sName

/* THIS INSERTS UNICODE */
SET @iID = 2
SET @sName = N'QQQ
EXECUTE @RC = MyTable_Insert @iID, @sName
GO

SELECT * FROM MyTable;
Jesse Houwing - 29 Sep 2007 11:13 GMT
Hello Hamilton Colyer-Long,

> I've reworked the code in the original post slightly to better
> illustrate my
[quoted text clipped - 28 lines]
> GO
> SELECT * FROM MyTable;

C# will correctly pass teh string as unicode if you explain it by setting
the correct DBType in the SqlParameter class (which will probably be set
automagically if you leave it out).

The fact why you['re getting questionmarks in your database is indeed because
you're not putting the N in front of the string.

If you make the call from C#, it will see that the parameter is in unicode
and pass the string on as is (by default C#, under the hood, uses unicode
for strings)

--
Jesse Houwing
jesse.houwing at sogeti.nl
Hamilton Colyer-Long - 29 Sep 2007 12:51 GMT
Ok, that makes sense. This may be the wrong forum now for my question but
can you tell me what dbtype to use in C# to support unicode? I have a table
adapter which has the "Name" field with DataType "System.String". The stored
procedure to insert a new record has a parameter for this field with DBType
"AnsiString" and no ProviderType specifed. This doesn't work, and neither
does any other combination I've tried. TIA.
Jesse Houwing - 29 Sep 2007 14:24 GMT
Hello Hamilton Colyer-Long,

> Ok, that makes sense. This may be the wrong forum now for my question
> but can you tell me what dbtype to use in C# to support unicode? I
[quoted text clipped - 3 lines]
> specifed. This doesn't work, and neither does any other combination
> I've tried. TIA.

You need to set the DBType of the parameter to:

DBType.String   
A type representing Unicode character strings.
http://msdn2.microsoft.com/en-us/library/system.data.dbtype.aspx

Or if you're using SqlDbType

SqlDBType.NVarChar   
String. A variable-length stream of Unicode characters ranging between 1
and 4,000 characters. Implicit conversion fails if the string is greater
than 4,000 characters. Explicitly set the object when working with strings
longer than 4,000 characters.
http://msdn2.microsoft.com/en-us/library/system.data.sqldbtype.aspx

I'd guess that the microsoft.public.dotnet.framework.adonet newsgroup would've
been the better choice for this question.

--
Jesse Houwing
jesse.houwing at sogeti.nl
Hamilton Colyer-Long - 29 Sep 2007 14:32 GMT
Hi Jesse,

I had read that article since my last post and switched to DbType.String
with no success. My app still supports unicode with Jet but not with SQL
Server. Anyway, I will move this line of query to another group. Thanks for
your help today.

PS I'll continue to monitor this thread in case anyone has an answer. Would
still greatly appreciate a solution.

Regards,
Hamilton
Hamilton Colyer-Long - 29 Sep 2007 15:32 GMT
Fixed now.
Solution: Have you tried turning it off then on again?
Dan Guzman - 29 Sep 2007 12:35 GMT
> NOTE: Prefixing the variable name with an "N" does fix the problem:
>  SET @sName = '??'     // this inserts ??
[quoted text clipped - 3 lines]
> 'automatic' detection of string variables should make this unecessary
> right?

As Jesse mentioned, strings in C# are Unicode.  You'll get the desired
behavior as long as you parameterize your SQL statements with the correct
datatype.

Signature

Hope this helps.

Dan Guzman
SQL Server MVP

> Hi,
>
[quoted text clipped - 49 lines]
> Janusys
> www.janusys.com.au
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.