好久没发帖了,设计也快做完了,本来已经实现了在comboBox显示学生名字的功能,但今天看了一个程序,发现可以更加完善的,就是先把数据库中的名字(Stu_Name)的拼音的首字母提取出来,显示在comboBox1中,然后根据选中的首字母,显示同样首字母的所有名字在comboBox2中,现在不知道怎么把拼音的首字母提取出来放在comboBox中?
表为StuInfo,表的设计为:
Class_ID Stu_Name Mingci
1 王 1
1 李 2
1 红 3
1 刚 4
1 明 5
1 花 6
2 丽 1
2 佘 2
2 邓 3
2 黄 4
.................... 注:用C#.Net 2003 和SQL Server 2000 做的
表为StuInfo,表的设计为:
Class_ID Stu_Name Mingci
1 王 1
1 李 2
1 红 3
1 刚 4
1 明 5
1 花 6
2 丽 1
2 佘 2
2 邓 3
2 黄 4
.................... 注:用C#.Net 2003 和SQL Server 2000 做的
在表中设计:编号(int) 字母(char)
1 A
2 B
然后在StuInfo中添加首字母代码行,这样你就可以在程序加载的时候,
将StuInfo的字母代码加载到combobox1中,当你选择首字母时候,就可以用代码实现加载到combobox2中了.
其实就是写两条SQL的搜索语句实现的.
{
comboBox4.Items.Clear(); comboBox4.Enabled = true;
string Afile = "provider=microsoft.jet.OLEDB.4.0;Data source=" + name.into.nto("ServerPath") + "";
OleDbConnection AconnStr = new OleDbConnection(Afile);
OleDbCommand scmd = new OleDbCommand("select top 200 * from cattle where 镇区='" + comboBox2.Text.Trim() + "' order by 编号", AconnStr);
OleDbDataReader odr = null;
AconnStr.Open();
odr = scmd.ExecuteReader();
if (odr != null)
{
while (odr.Read())
{
comboBox4.Items.Add(odr["网吧名称"].ToString());
}
odr.Close();
}
comboBox4.Text = ""; }
取得汉字的拼音的首字母
static public string GetChineseSpell(string strText)
{
int len = strText.Length;
string myStr = "";
for(int i=0;i<len;i++)
{
myStr += getSpell(strText.Substring(i,1));
}
return myStr;
} static public string getSpell(string cnChar)
{
byte[] arrCN = Encoding.Default.GetBytes(cnChar);
if(arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area<<8) + pos;
int[] areacode = {45217,45253,45761,46318,46826,47010,47297,47614,48119,48119,49062,49324,49896,50371,50614,50622,50906,51387,51446,52218,52698,52698,52698,52980,53689,54481};
for(int i=0;i<26;i++)
{
int max = 55290;
if(i != 25) max = areacode[i+1];
if(areacode[i]<=code && code<max)
{
return Encoding.Default.GetString(new byte[]{(byte)(65+i)});
}
}
return "*";
}
else return cnChar;
}
area < <8---〉area <<8
http://www.cnblogs.com/anjou/archive/2007/09/29/911273.html