3

I am attempting to use CollationInfo.Comparer from SMO to get my c# code to sort like SQL Server. I have gotten the correct collation, but my items still do not sort correctly.

var collationInfo = CollationInfo.Collations.Single(x => x.Name == "SQL_Latin1_General_CP1_CS_AS") as CollationInfo;
var comparer = collationInfo.Comparer;

int c = comparer.Compare("Tri-Valley L", "Trimble L");

In this case c returns '1' indicating that Tri-Valley L will come after Trimble.

However this code in SQL Server

DECLARE @T TABLE
(
    Name VARCHAR(20)
)

INSERT INTO @T
(
    Name
)
VALUES('Tri-Valley L'),
    ('Trimble L')

SELECT
Name
FROM
@T
ORDER BY Name

Returns Tri-Valley before Trimble.

Does the collation compare stuff just not work correctly, or am I doing something wrong?

4

1 回答 1

1

旧版“SQL”排序规则与 Windows“字排序”算法不一致。您需要对数据库中的列使用 Windows 排序规则(例如 Latin1_General_CS_AS)以获得相同的行为。

于 2014-07-20T19:35:22.933 回答