这里用到几个函数,一个SQL自定义函数,主要按汉语拼音字母排序,这个函数来源于网上收集,记不得作者是谁了,还有那个取汉字首字母(大写),首先在这里感谢他们。

Create  function GetPy(@str nvarchar(4000))
returns nvarchar(4000)      
  
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字字符,返回原字符
set @PY=@PY+(case when unicode(@wordbetween 19968 and 19968+20901
then (select top 1 PY from (
select 'A' as PY,N'' as word
union all select 'B',N'簿'
union all select 'C',N''
union all select 'D',N''
union all select 'E',N''
union all select 'F',N''
union all select 'G',N''
union all select 'H',N''
union all select 'J',N''
union all select 'K',N''
union all select 'L',N''
union all select 'M',N''
union all select 'N',N''
union all select 'O',N''
union all select 'P',N''
union all select 'Q',N''
union all select 'R',N''
union all select 'S',N''
union all select 'T',N''
union all select 'W',N''
union all select 'X',N''
union all select 'Y',N''
union all select 'Z',N''
) T 
where word>=@word collate Chinese_PRC_CS_AS_KS_WS 
order by PY ASCelse @word end)
set @str=right(@str,len(@str)-1)
end
return @PY
end

然后在查询进,用这样SQL语句

Select Id,Title From Table_a order by dbo.getpy(title)

这样做后TITLE已经按汉语拼音字母顺序排列了,
还有一个取汉字取拼音首字母(大写) 函数

 1 Public Shared Function Extract_HZ(ByVal HZ As StringAs String
 2                Dim ZW(2As Byte
 3                Dim HZ_INT As Long
 4                ZW = System.Text.Encoding.Default.GetBytes(HZ)
 5                Dim i1 As Integer = CType((ZW(0)), Short)
 6                Dim i2 As Integer = CType((ZW(1)), Short)
 7                HZ_INT = i1 * 256 + i2
 8                If (HZ_INT >= 45217AndAlso (HZ_INT <= 45252Then
 9                    Return "A"
10                End If
11                If (HZ_INT >= 45253AndAlso (HZ_INT <= 45760Then
12                    Return "B"
13                End If
14                If (HZ_INT >= 45761AndAlso (HZ_INT <= 46317Then
15                    Return "C"
16                End If
17                If (HZ_INT >= 46318AndAlso (HZ_INT <= 46825Then
18                    Return "D"
19                End If
20                If (HZ_INT >= 46826AndAlso (HZ_INT <= 47009Then
21                    Return "E"
22                End If
23                If (HZ_INT >= 47010AndAlso (HZ_INT <= 47296Then
24                    Return "F"
25                End If
26                If (HZ_INT >= 47297AndAlso (HZ_INT <= 47613Then
27                    Return "G"
28                End If
29                If (HZ_INT >= 47614AndAlso (HZ_INT <= 48118Then
30                    Return "H"
31                End If
32                If (HZ_INT >= 48119AndAlso (HZ_INT <= 49061Then
33                    Return "J"
34                End If
35                If (HZ_INT >= 49062AndAlso (HZ_INT <= 49323Then
36                    Return "K"
37                End If
38                If (HZ_INT >= 49324AndAlso (HZ_INT <= 49895Then
39                    Return "L"
40                End If
41                If (HZ_INT >= 49896AndAlso (HZ_INT <= 50370Then
42                    Return "M"
43                End If
44                If (HZ_INT >= 50371AndAlso (HZ_INT <= 50613Then
45                    Return "N"
46                End If
47                If (HZ_INT >= 50614AndAlso (HZ_INT <= 50621Then
48                    Return "O"
49                End If
50                If (HZ_INT >= 50622AndAlso (HZ_INT <= 50905Then
51                    Return "P"
52                End If
53                If (HZ_INT >= 50906AndAlso (HZ_INT <= 51386Then
54                    Return "Q"
55                End If
56                If (HZ_INT >= 51387AndAlso (HZ_INT <= 51445Then
57                    Return "R"
58                End If
59                If (HZ_INT >= 51446AndAlso (HZ_INT <= 52217Then
60                    Return "S"
61                End If
62                If (HZ_INT >= 52218AndAlso (HZ_INT <= 52697Then
63                    Return "T"
64                End If
65                If (HZ_INT >= 52698AndAlso (HZ_INT <= 52979Then
66                    Return "W"
67                End If
68                If (HZ_INT >= 52980AndAlso (HZ_INT <= 53640Then
69                    Return "X"
70                End If
71                If (HZ_INT >= 53689AndAlso (HZ_INT <= 54480Then
72                    Return "Y"
73                End If
74                If (HZ_INT >= 54481AndAlso (HZ_INT <= 55289Then
75                    Return "Z"
76                End If
77                '  Return ("")
78                Return (UCase(Left(HZ, 1)))
79            End Function

接着写一个函数接入字母分隔线

 1 Private Function DownListInsertZM(ByVal DropDownListId As DropDownList, ByVal InsertHH As StringAs DropDownList
 2         Dim j, s, i, c, r As Integer
 3         Dim tempdual As String
 4         For c = 65 To 90 '大写ASCII码循环
 5             i = 0
 6             For j = 0 To DropDownListId.Items.Count - 1
 7                 tempdual = Extract_HZ(DropDownListId.Items(j).Text).Trim
 8                 If tempdual = Chr(c) Then
 9                     i = i + 1  '统计相同项数
10                     s = j
11                 End If
12             Next
13             r = s - i + 1  '计算插入位置的索引号
14             If r < 0 Then
15                 r = 0
16             End If
17             If i <> 0 Then
18                 DropDownListId.Items.Insert(r, New ListItem((InsertHH & Chr(c) & InsertHH), "*"))
19             End If
20         Next
21         DropDownListId.Items.Insert(0New ListItem((InsertHH & "请选择" & InsertHH)))       '插入"请选择"为Dropdownlist为第一项
22     End Function
23 

最后便可实现如下效果:
 ---请选择---
  ----A----
  啊
  爱国者
  ----B----
  变速器
  ................
 
        笔者后来想了一下,也可以在DataTable实现这个操作,因我是用DataReader的方法读数据,所在后面这个插入分隔线的操作应该在数据绑定之后再进行,如果在DATATABLE中就插好了,可以直接绑定。待有时间写个DataTable的。小弟第一次发文章,请各位指教。

posted on 2006-05-11 14:04  PetterLiu  阅读(1581)  评论(1编辑  收藏  举报