带有问号的nvarchar列结果

时间:2017-12-24 13:33:03

标签: c# sql character-encoding sql-server-2008-r2

我正在尝试更新队列项并检索它的列文本内容。

问题是希伯来字符等特殊标志导致了问号:????

我可以通过直接SELECT子句(在sql management studio内)看到文字完美无缺:

Message's column
-------
היי     
hey

当我尝试检索数据时,它会被扰乱:

 היי     --->    ??? (Not OK)
 hey     --->    hey (OK)

我的表:

CREATE TABLE [dbo].[MyQueue](   
    [Message] [nvarchar](1000) NOT NULL 
    --some additional columns
)

这是我的存储过程:

ALTER procedure [dbo].[MyDequeue] (
)
as
begin
    with CTE as (
            SELECT TOP (100) *
            FROM MyQueue WITH (xlock,READPAST)
            WHERE Locked = 0
            and HasError=0
            and Success=0)
        UPDATE CTE
        SET Locked = 1, LockTime=getUtcDate()
        OUTPUT INSERTED.*;
end

我正在通过这些功能阅读这个项目:

    public IEnumerable<MyQueue> Dequeue(int batchSize)
    {
        var cmd = dataManager.CreateCommand();
        cmd.CommandText = "MyDequeue";
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        using (var reader = dataManager.ExecuteReader(cmd))
        {
            var ordinals = reader.LoadFields();
            List<MyQueue> items = new List<MyQueue>();
            while (reader.Read())
            {
                items.Add(new MyQueue()
                {
                    Message = reader.GetString(ordinals["Message"])
                    // some additional properties init
                });
            }
            return items;
        }
    }


    public static Dictionary<string, int> LoadFields(this IDataReader reader)
    {
        Dictionary<string, int> loadedColumns = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
        for (int i = 0; i < reader.FieldCount; i++)
        {
            try
            {
                loadedColumns.Add(reader.GetName(i), i);
            }
            catch (System.ArgumentException) { }
        }
        return loadedColumns;
    }

1 个答案:

答案 0 :(得分:0)

已解决(归功于@ dan-guzman):

需要插入带有参数化查询和字符文字N前缀的数据。

like:N 'היי'