开发者

How do I insert unicode into MS-SQL?

开发者 https://www.devze.com 2023-01-08 16:35 出处:网络
I want to insert info.NativeName into a nvarchar field in the database. It doesn\'t work, all I get is ??????? where the encoding is not western/latin.

I want to insert info.NativeName into a nvarchar field in the database. It doesn't work, all I get is ??????? where the encoding is not western/latin.

Outputting listcultures directly in an asp.net website on page_onload worked fine, but it seems not to work via database.

Public Sub listcultures()
    'Dim x As System.DateTime = DateTime.Now
    'Response.Write(x.ToString("HH':'mm':'ss MMM d', 'yyy 'PST'", New System.Globalization.CultureInfo("zh-CN", False)))
    Dim info As System.Globalization.CultureInfo
    For Each info In System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
        'Response.Write("Deutsch: " + info.DisplayName + " English: " + info.EnglishName + " Native: " + info.NativeName + " Name: " + info.Name + " Codepage: " + info.TextInfo.ANSICodePage.ToString() + "<br />")

        'InsertData(info.DisplayName, info.EnglishName, info.NativeName, info.Name, info.TextInfo.ANSICodePage.ToString(), info.IsNeutralCulture.ToString())

        If Not info.IsNeutralCulture Then
            'item.SubItems.Add(amount.ToString("C", info.NumberFormat))
 开发者_Python百科           'item.SubItems.Add(dateNow.ToString("d", info.DateTimeFormat))
        End If
    Next

End Sub

What am I doing wrong? I suppose something with encoding ?


You need to add N before the unicode string when you are inserting it. You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server


What exactly happens? Ultimately you are just building a string here and storing into nvarchar, so it should work fine, provided:

  • you have correctly parametrized the input to the command
  • you haven't overflown the width of the the column

We can't really say more without seeing InsertData, which I assume talks to the database. I'm also not quite sure why a "list" method would be doing inserts in the first place, of course. Plus since everything is commented out at the moment, I would expect nothing to happen...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号