A.G.
A.G.

Reputation: 2149

c# not translating unicode string

I have this string that I want to insert into a table:

 string command = @" insert into " + txnDC.Connection.Database + "..[AZGetPackages] ( [PackageID] , [PackageName] , [VendorID] , [VendorDisplayName] , [IsCustom] , [EPCustomerID] )" +
                    @"values( N'1', N'Package 1', N'11', N'Vendor 1', N'0', N''),
                    ( N'2', N'Package 2', N'12', N'Vendor 2', N'0', N''),
                    ( N'3', N'Package 3', N'13', N'Vendor 3', N'0', N''),
                    ( N'4', N'雑誌コード', N'14', N'税込', N'0', N'')";

When I put a breakpoint right after the that line and I view the string in debug it shows up like this:

 txnDC.ExecuteCommand(command, new object[0]); << breakpoint here to view command var

and shows || for the chinese chars.

The pipes are actually those little rectangles.

So when it does the insert to the table it inserts the rectangle characters and not the actual ones I have set.

Anyone have any advice here?

Thanks!

Upvotes: 3

Views: 217

Answers (2)

A.G.
A.G.

Reputation: 2149

I have run the exact same code on someone else's machine and it works there as expected, without any changes. This seems to be a display issue on my system. The only out of the ordinary thing that happened was that I went away on vacation and left my machine off for 10 days. I know that's probably irrelevant but it seems to have done something to my system related to fonts. And no one touched my machine while I was away, and I didn't do anything font related before I went away.

But there's a twist, Notepad won't display the chinese chars, neither will the VS debugger, not the SQL SSMS grid results view. But VS .CS file string var def will and If I paste into the SSMS editor it will show them too.

Insane!

Upvotes: 1

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

OK, I'm not sure what you're doing exactly, but you'll be better off using parameterized queries. So maybe something like this (this is pseudo-code so it will need modified to compile):

using (SqlConnection c = new SqlConnection("..."))
{
    c.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO table (...) VALUES (@field1, ...)", c);
    {
        cmd.Parameters.AddWithValue("@field1", "税込");
        cmd.Parameters[0].SqlDbType = SqlDbType.NVarChar;

        cmd.ExecuteNonQuery();
    }
}

Upvotes: 0

Related Questions