CSDN博客

img fengyun14

C#学习心得

发表于2004/10/19 20:26:00  4602人阅读

分类: ASP.NET

C# SQL数据库在外地连接解决方法:

Form1:
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 F2=new Form2();
F2.ShowDialog();
if(F2.DialogResult==DialogResult.OK)
{
string cnStr="uid="+F2.stru1.user+";password="+F2.stru1.pw+";database="+F2.stru1.dbase+
";server=localhost";
SqlConnection cn=new SqlConnection(cnStr);
}
}

Form2:
1、先建立一个struct,并构造:
public struct stru
{
public string dbase,user,pw;
public stru(string Dbase,string User,string Pw)
{
dbase=Dbase;user=User;pw=Pw;
}
}
public stru stru1=new stru("","","");
2、在Form2上建立2个按钮,一个“确定”按钮,其dialogResult设为None;另一个“取消”按钮,其dialogResult设为“Cancel”;
private void button1_Click(object sender, System.EventArgs e)
{
stru1.dbase=tb1.Text;
stru1.user=tb2.Text;
stru1.pw=tb3.Text;
string cnStr="uid="+stru1.user+";password="+stru1.pw+";database="+stru1.dbase+";server=localhost";
cn.ConnectionString=cnStr;
try
{
cn.Open();
MessageBox.Show("OK,您的数据库连接成功!");
this.DialogResult=DialogResult.OK;
//在FORM1中,DialogResult只有在不是None的情况下,可以接收已经关闭的Form2的存储的结果
//另外,如果按钮没有设置,那么虽然按钮的DialogResult=None,但窗体却是Cancel.
this.Close();//这样就可以达到如果数据库连接失败,就不关闭此窗口,连接成功,才关闭此窗口。
}
catch (Exception ex)
{
MessageBox.Show("对不起,你的数据库连接失败!");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
stru1.dbase="";
stru1.user="";
stru1.pw="";
}

 

DataList的翻页问题:

<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load( Object sender , EventArgs e)
{
//Build the Grid only if the page has been accessed for the first time
if( !IsPostBack )
BuildGrid();
}

public void BuildGrid()
{
SqlConnection myConnection =
new SqlConnection(
"server=(local)//NetSDK;database=Northwind;Trusted_Connection=yes" );
SqlDataAdapter myAdapter =
new SqlDataAdapter(
"SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products" ,
myConnection);
//Fill the DataSet
DataSet ds = new DataSet();
//Get the startRecord Count
//Remember database count is zero based so first decrease the value of
//the current page
int startRecord = ( int.Parse( CurrentPage.Value ) - 1 ) *
int.Parse( PageSize.Value );
//Fetch only the necessary records.
myAdapter.Fill( ds , startRecord , int.Parse( PageSize.Value ) ,
"Products");
//DataBind the Repeater
MyRepeater.DataSource = ds.Tables["Products"].DefaultView;
MyRepeater.DataBind();

//Second Part
//Create a new Command to select the total number of records
SqlCommand myCmd = new SqlCommand( "SELECT Count(*) from Products",
myConnection );
myConnection.Open();
//retrieve the value
TotalSize.Value = myCmd.ExecuteScalar().ToString() ;
myConnection.Close();
BuildPagers();
}

public void Page_Repeater( object sender, EventArgs e )
{
//Check for Button clicked
if( ((LinkButton)sender).ID == "Prev" )
{
//Check if we are on any page greater than 0
if( ( int.Parse( CurrentPage.Value ) - 1 ) >= 0 )
{
//Decrease the CurrentPage Value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) - 1 ).ToString() ;
}
}
else if( ((LinkButton)sender).ID == "Next" )
{
//Check if we can display the next page.
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
< int.Parse( TotalSize.Value ) )
{
//Increment the CurrentPage value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) + 1 ).ToString() ;
}
}
//Rebuild the Grid
BuildGrid();
}

public void BuildPagers()
{
//Check if its possible to have the previous page
if( ( int.Parse( CurrentPage.Value ) - 1 ) <= 0 )
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true ;
}
//Check if its possible to have the next page
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
>= int.Parse( TotalSize.Value ) )
{
Next.Enabled = false;
}
else
{
Next.Enabled = true ;
}
}
</script>
<body>
<h1>Products Listing</h1>
<form runat="server">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<table width="100%" border="1" cellpadding="1" cellspacing="2">
<tr>
<th>
Product ID
</th>
<th>
Product
</th>
<th>
Quantity Per Unit
</th>
<th>
Unit Price
</th>
</tr>

</HeaderTemplate>

<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "ProductID") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %>
</td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</ASP:Repeater>
<input type="hidden" id="PageSize" value="10" runat="server">
<input type="hidden" id="CurrentPage" value="1" runat="server">
<input type="hidden" id="TotalSize" runat="server">
<asp:LinkButton id="Prev" Text="<< Previous" OnClick="Page_Repeater" runat="server" />
&nbsp;
<asp:LinkButton id="Next" Text="Next >>" OnClick="Page_Repeater" runat="server" />
</form>
</body>
</html>

 

很强的c#.net函数列表


1、DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)

2、Int32.Parse(变量) Int32.Parse("常量")
字符型转换 转为32位数字型

3、 变量.ToString()
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%


4、变量.Length 数字型
取字串长度:
如: string str="中国";
int Len = str.Length ; //Len是自定义变量, str是求测的字串的变量名

5、System.Text.Encoding.Default.GetBytes(变量)
字码转换 转为比特码
如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
然后可得到比特长度:
len = bytStr.Length;

6、System.Text.StringBuilder("")
字符串相加,(+号是不是也一样?)
如:System.Text.StringBuilder sb = new System.Text.StringBuilder("");
sb.Append("中华");
sb.Append("人民");
sb.Append("共和国");

7、变量.Substring(参数1,参数2);
截取字串的一部分,参数1为左起始位数,参数2为截取几位。
如:string s1 = str.Substring(0,2);

8、String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();
取远程用户IP地址

9、穿过代理服务器取远程用户真实IP地址:
if(Request.ServerVariables["HTTP_VIA"]!=null){
string user_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}else{
string user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();
}

10、 Session["变量"];
存取Session值;
如,赋值: Session["username"]="小布什";

取值: Object objName=Session["username"];
String strName=objName.ToString();
清空: Session.RemoveAll();

11、String str=Request.QueryString["变量"];
用超链接传送变量。
如在任一页中建超链接:<a href=Edit.aspx?fbid=23>点击</a>
在Edit.aspx页中取值:String str=Request.QueryString["fdid"];

12、DOC对象.CreateElement("新建节点名");
创建XML文档新节点

13、父节点.AppendChild(子节点);
将新建的子节点加到XML文档父节点下

14、 父节点.RemoveChild(节点);
删除节点

15、Response
Response.Write("字串");
Response.Write(变量);
向页面输出。

Response.Redirect("URL地址");
跳转到URL指定的页面

16、char.IsWhiteSpce(字串变量,位数)——逻辑型
查指定位置是否空字符;
如:
string str="中国 人民";
Response.Write(char.IsWhiteSpace(str,2)); //结果为:True, 第一个字符是0位,2是第三个字符。

17、char.IsPunctuation('字符') --逻辑型
查字符是否是标点符号
如:Response.Write(char.IsPunctuation('A')); //返回:False

18、(int)'字符'
把字符转为数字,查代码点,注意是单引号。
如:
Response.Write((int)'中'); //结果为中字的代码:20013

19、(char)代码
把数字转为字符,查代码代表的字符。
如:
Response.Write((char)22269); //返回“国”字。

20、 Trim()
清除字串前后空格

21 、字串变量.Replace("子字串","替换为")
字串替换
如:
string str="中国";
str=str.Replace("国","央"); //将国字换为央字
Response.Write(str); //输出结果为“中央”

再如:(这个非常实用)

string str="这是<script>脚本";
str=str.Replace("<","<font><</font>"); //将左尖括号替换为<font> 与 < 与 </font> (或换为<,但估计经XML存诸后,再提出仍会还原)
Response.Write(str); //显示为:“这是<script>脚本”

如果不替换,<script>将不显示,如果是一段脚本,将运行;而替换后,脚本将不运行。
这段代码的价值在于:你可以让一个文本中的所有HTML标签失效,全部显示出来,保护你的具有交互性的站点。
具体实现:将你的表单提交按钮脚本加上下面代码:
string strSubmit=label1.Text; //label1是你让用户提交数据的控件ID。
strSubmit=strSubmit.Replace("<","<font><</font>");
然后保存或输出strSubmit。
用此方法还可以简单实现UBB代码。

22、Math.Max(i,j)
取i与j中的最大值
如 int x=Math.Max(5,10); // x将取值 10

23、字串对比一般都用: if(str1==str2){ } , 但还有别的方法:

(1)、
string str1; str2
//语法: str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值.如:
if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); }

(2)、
//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上.

(3)、
//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上.

24、IndexOf() 、LastIndexOf()
查找字串中指定字符或字串首次(最后一次)出现的位置,返回索引值,如:
str1.IndexOf("字"); //查找“字”在str1中的索引值(位置)
str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置)
str1.IndexOf("字串",3,2);//从str1第4个字符起,查找2个字符,查找“字串”的第一个字符在str1中的索引值(位置)

25、Insert()
在字串中指定索引位插入指定字符。如:
str1.Insert(1,"字");在str1的第二个字符处插入“字”,如果str1="中国",插入后为“中字国”;

26、PadLeft()、PadRight()
在字串左(或右)加空格或指定char字符,使字串达到指定长度,如:
<%
string str1="中国人";
str1=str1.PadLeft(10,'1'); //无第二参数为加空格
Response.Write(str1); //结果为“1111111中国人” , 字串长为10
%>

27、Remove()
从指定位置开始删除指定数的字符
字串对比一般都用: if(str1==str2){ } , 但还有别的方法:

1、
string str1; str2
//语法: str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值.如:
if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); }

2、
//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上.

3、
//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上.

IndexOf()
查找字串中指定字符或字串首次出现的位置,返首索引值,如:
str1.IndexOf("字"); //查找“字”在str1中的索引值(位置)
str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置)
str1.IndexOf("字串",3,2);//从str1第4个字符起,查找2个字符,查找“字串”的第一个字符在str1中的索引值(位置)

1.9 取中文日期显示——年月日时分
string strY=currentTime.ToString("f"); //不显示秒

1.10 取中文日期显示_年月
string strYM=currentTime.ToString("y");

1.11 取中文日期显示_月日
string strMD=currentTime.ToString("m");

1.12 取当前年月日,格式为:2003-9-23
string strYMD=currentTime.ToString("d");

1.13 取当前时分,格式为:14:24
string strT=currentTime.ToString("t");

protected DateTime TimeOfDayDateTime()
{
DateTime dtNow = DateTime.Now;
//返回时间
DateTime dtCompare = new DateTime(dtNow.Year,dtNow.Month,dtNow.Day);
return dtCompare.AddMinutes(this.TimeOfDay);
}


 

标题 用C#实现汉字转化为拼音 chinahuman(原作)

关键字 汉字 拼音 转换


其实现的原理就是先将汉字转化成为内码,然后通过内码和拼音的对照来查找。

详细的代码如下所示:

public class chs2py
{/// <summary>
///将汉字转换成为拼音
///作者:铁匠(chinahuman)
///完成于:2004年3月12日早上
/// </summary>

private static int[] pyvalue=new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,
-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,
-19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,
-19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,
-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526,
-18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
-18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,
-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,
-17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,
-16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,
-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,
-15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,
-15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,
-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,
-14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,
-14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,
-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,
-14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,
-13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,
-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,
-12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,
-12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,
-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,
-11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,
-10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254};
private static string[] pystr=new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",
"bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",
"cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",
"cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",
"dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",
"ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",
"hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
"jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",
"keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",
"leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
"ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
"na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
"nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",
"pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
"ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",
"sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",
"shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",
"tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
"wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",
"xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",
"yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",
"zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
"zi","zong","zou","zu","zuan","zui","zun","zuo"};
public chs2py()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string convert(string chrstr)
{
byte[] array = new byte[2];
string returnstr="";
int chrasc=0;
int i1 = 0;
int i2 =0;
char[] nowchar=chrstr.ToCharArray();
for (int j=0;j<nowchar.Length ;j++)
{
array = System.Text.Encoding.Default.GetBytes(nowchar[j].ToString ());
i1 = (short)(array[0] );
i2 = (short)(array[1] );

chrasc=i1*256+i2-65536;
if (chrasc>0 && chrasc<160)
{
returnstr+=nowchar[j];
}
else
{
for (int i=(pyvalue.Length -1);i>=0;i--)
{
if (pyvalue[i]<=chrasc)
{
returnstr+=pystr[i];
break;
}
}
}
}
return returnstr;

}
}

 

 

在C#中取汉字的拼音码
大同市远大软件公司 张和平 www.ydsoft.com.cn

在C#中取汉字的拼音码,支持数字、英文字符、一级汉字、二级汉字


public string GetFirstLetter(string hz)
{

string ls_second_eng = "CJWGNSPGCGNESYPBTYYZDXYKYGTDJNNJQMBSGZSCYJSYYQPGKBZGYCYWJKGKLJSWKPJQHYTWDDZLSGMRYPYWWCCKZNKYDGTTNGJEYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJZXDTTSQZYCDXXHGCKBPHFFSSWYBGMXLPBYLLLHLXSPZMYJHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZCDJZWQJBDZBXGZNZCPWHKXHQKMWFBPBYDTJZZKQHYLYGXFPTYJYYZPSZLFCHMQSHGMXXSXJJSDCSBBQBEFSJYHWWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBNPTSDKDYLHGYMYLCXPYCJNDQJWXQXFYYFJLEJBZRXCCQWQQSBNKYMGPLBMJRQCFLNYMYQMSQTRBCJTHZTQFRXQ"+
"HXMJJCJLXQGJMSHZKBSWYEMYLTXFSYDSGLYCJQXSJNQBSCTYHBFTDCYZDJWYGHQFRXWCKQKXEBPTLPXJZSRMEBWHJLBJSLYYSMDXLCLQKXLHXJRZJMFQHXHWYWSBHTRXXGLHQHFNMNYKLDYXZPWLGGTMTCFPAJJZYLJTYANJGBJPLQGDZYQYAXBKYSECJSZNSLYZHZXLZCGHPXZHZNYTDSBCJKDLZAYFMYDLEBBGQYZKXGLDNDNYSKJSHDLYXBCGHXYPKDJMMZNGMMCLGWZSZXZJFZNMLZZTHCSYDBDLLSCDDNLKJYKJSYCJLKOHQASDKNHCSGANHDAASHTCPLCPQYBSDMPJLPCJOQLCDHJJYSPRCHNWJNLHLYYQYYWZPTCZGWWMZFFJQQQQYXACLBHKDJXDGMMYDJXZLLSYGXGKJRYWZWYCLZMSSJZLDBYDCFCXYHLXCHYZJQSFQAGMNYXPFRKSSB"+
"JLYXYSYGLNSCMHCWWMNZJJLXXHCHSYDSTTXRYCYXBYHCSMXJSZNPWGPXXTAYBGAJCXLYSDCCWZOCWKCCSBNHCPDYZNFCYYTYCKXKYBSQKKYTQQXFCWCHCYKELZQBSQYJQCCLMTHSYWHMKTLKJLYCXWHEQQHTQHZPQSQSCFYMMDMGBWHWLGSSLYSDLMLXPTHMJHWLJZYHZJXHTXJLHXRSWLWZJCBXMHZQXSDZPMGFCSGLSXYMJSHXPJXWMYQKSMYPLRTHBXFTPMHYXLCHLHLZYLXGSSSSTCLSLDCLRPBHZHXYYFHBBGDMYCNQQWLQHJJZYWJZYEJJDHPBLQXTQKWHLCHQXAGTLXLJXMSLXHTZKZJECXJCJNMFBYCSFYWYBJZGNYSDZSQYRSLJPCLPWXSDWEJBJCBCNAYTWGMPAPCLYQPCLZXSBNMSGGFNZJJBZSFZYNDXHPLQKZCZWALSBCCJXJYZGWKYP"+
"SGXFZFCDKHJGXDLQFSGDSLQWZKXTMHSBGZMJZRGLYJBPMLMSXLZJQQHZYJCZYDJWBMJKLDDPMJEGXYHYLXHLQYQHKYCWCJMYYXNATJHYCCXZPCQLBZWWYTWBQCMLPMYRJCCCXFPZNZZLJPLXXYZTZLGDLDCKLYRZZGQTGJHHHJLJAXFGFJZSLCFDQZLCLGJDJCSNCLLJPJQDCCLCJXMYZFTSXGCGSBRZXJQQCTZHGYQTJQQLZXJYLYLBCYAMCSTYLPDJBYREGKLZYZHLYSZQLZNWCZCLLWJQJJJKDGJZOLBBZPPGLGHTGZXYGHZMYCNQSYCYHBHGXKAMTXYXNBSKYZZGJZLQJDFCJXDYGJQJJPMGWGJJJPKQSBGBMMCJSSCLPQPDXCDYYKYFCJDDYYGYWRHJRTGZNYQLDKLJSZZGZQZJGDYKSHPZMTLCPWNJAFYZDJCNMWESCYGLBTZCGMSSLLYXQSXSBSJS"+
"BBSGGHFJLWPMZJNLYYWDQSHZXTYYWHMCYHYWDBXBTLMSYYYFSXJCSDXXLHJHFSSXZQHFZMZCZTQCXZXRTTDJHNNYZQQMNQDMMGYYDXMJGDHCDYZBFFALLZTDLTFXMXQZDNGWQDBDCZJDXBZGSQQDDJCMBKZFFXMKDMDSYYSZCMLJDSYNSPRSKMKMPCKLGDBQTFZSWTFGGLYPLLJZHGJJGYPZLTCSMCNBTJBQFKTHBYZGKPBBYMTTSSXTBNPDKLEYCJNYCDYKZDDHQHSDZSCTARLLTKZLGECLLKJLQJAQNBDKKGHPJTZQKSECSHALQFMMGJNLYJBBTMLYZXDCJPLDLPCQDHZYCBZSCZBZMSLJFLKRZJSNFRGJHXPDHYJYBZGDLQCSEZGXLBLGYXTWMABCHECMWYJYZLLJJYHLGBDJLSLYGKDZPZXJYYZLWCXSZFGWYYDLYHCLJSCMBJHBLYZLYCBLYDPDQYSXQZB"+
"YTDKYXJYYCNRJMDJGKLCLJBCTBJDDBBLBLCZQRPXJCGLZCSHLTOLJNMDDDLNGKAQHQHJGYKHEZNMSHRPHQQJCHGMFPRXHJGDYCHGHLYRZQLCYQJNZSQTKQJYMSZSWLCFQQQXYFGGYPTQWLMCRNFKKFSYYLQBMQAMMMYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYJDJJZZHQPDSZGLSTJBCKBXYQZJSGPSXQZQZRQTBDKYXZKHHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGSZZQJYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZM"+
"ALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNDSZFDEQFHBSAQTGLLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ";
string ls_second_ch = "亍丌兀丐廿卅丕亘丞鬲孬噩丨禺丿匕乇夭爻卮氐囟胤馗毓睾鼗丶亟"+
"鼐乜乩亓芈孛啬嘏仄厍厝厣厥厮靥赝匚叵匦匮匾赜卦卣刂刈刎刭刳刿剀剌剞剡剜蒯剽劂劁劐劓冂罔亻仃仉仂仨仡仫仞伛仳伢佤仵伥伧伉伫佞佧攸佚佝佟佗伲伽佶佴侑侉侃侏佾佻侪佼侬侔俦俨俪俅俚俣俜俑俟俸倩偌俳倬倏倮倭俾倜倌倥倨偾偃偕偈偎偬偻傥傧傩傺僖儆僭僬僦僮儇儋仝氽佘佥俎龠汆籴兮巽黉馘冁夔勹匍訇匐凫夙兕亠兖亳衮袤亵脔裒禀嬴蠃羸冫冱冽冼凇冖冢冥讠讦讧讪讴讵讷诂诃诋诏诎诒诓诔诖诘诙诜诟诠诤诨诩诮诰诳诶诹诼诿谀谂谄谇谌谏谑谒谔谕谖谙谛谘谝谟谠谡谥谧谪谫谮谯谲谳谵谶卩卺阝阢阡阱阪阽阼"+
"陂陉陔陟陧陬陲陴隈隍隗隰邗邛邝邙邬邡邴邳邶邺邸邰郏郅邾郐郄郇郓郦郢郜郗郛郫郯郾鄄鄢鄞鄣鄱鄯鄹酃酆刍奂劢劬劭劾哿勐勖勰叟燮矍廴凵凼鬯厶弁畚巯坌垩垡塾墼壅壑圩圬圪圳圹圮圯坜圻坂坩垅坫垆坼坻坨坭坶坳垭垤垌垲埏垧垴垓垠埕埘埚埙埒垸埴埯埸埤埝堋堍埽埭堀堞堙塄堠塥塬墁墉墚墀馨鼙懿艹艽艿芏芊芨芄芎芑芗芙芫芸芾芰苈苊苣芘芷芮苋苌苁芩芴芡芪芟苄苎芤苡茉苷苤茏茇苜苴苒苘茌苻苓茑茚茆茔茕苠苕茜荑荛荜茈莒茼茴茱莛荞茯荏荇荃荟荀茗荠茭茺茳荦荥荨茛荩荬荪荭荮莰荸莳莴莠莪莓莜莅荼莶莩荽莸荻"+
"莘莞莨莺莼菁萁菥菘堇萘萋菝菽菖萜萸萑萆菔菟萏萃菸菹菪菅菀萦菰菡葜葑葚葙葳蒇蒈葺蒉葸萼葆葩葶蒌蒎萱葭蓁蓍蓐蓦蒽蓓蓊蒿蒺蓠蒡蒹蒴蒗蓥蓣蔌甍蔸蓰蔹蔟蔺蕖蔻蓿蓼蕙蕈蕨蕤蕞蕺瞢蕃蕲蕻薤薨薇薏蕹薮薜薅薹薷薰藓藁藜藿蘧蘅蘩蘖蘼廾弈夼奁耷奕奚奘匏尢尥尬尴扌扪抟抻拊拚拗拮挢拶挹捋捃掭揶捱捺掎掴捭掬掊捩掮掼揲揸揠揿揄揞揎摒揆掾摅摁搋搛搠搌搦搡摞撄摭撖摺撷撸撙撺擀擐擗擤擢攉攥攮弋忒甙弑卟叱叽叩叨叻吒吖吆呋呒呓呔呖呃吡呗呙吣吲咂咔呷呱呤咚咛咄呶呦咝哐咭哂咴哒咧咦哓哔呲咣哕咻咿哌哙哚哜咩"+
"咪咤哝哏哞唛哧唠哽唔哳唢唣唏唑唧唪啧喏喵啉啭啁啕唿啐唼唷啖啵啶啷唳唰啜喋嗒喃喱喹喈喁喟啾嗖喑啻嗟喽喾喔喙嗪嗷嗉嘟嗑嗫嗬嗔嗦嗝嗄嗯嗥嗲嗳嗌嗍嗨嗵嗤辔嘞嘈嘌嘁嘤嘣嗾嘀嘧嘭噘嘹噗嘬噍噢噙噜噌噔嚆噤噱噫噻噼嚅嚓嚯囔囗囝囡囵囫囹囿圄圊圉圜帏帙帔帑帱帻帼帷幄幔幛幞幡岌屺岍岐岖岈岘岙岑岚岜岵岢岽岬岫岱岣峁岷峄峒峤峋峥崂崃崧崦崮崤崞崆崛嵘崾崴崽嵬嵛嵯嵝嵫嵋嵊嵩嵴嶂嶙嶝豳嶷巅彳彷徂徇徉後徕徙徜徨徭徵徼衢彡犭犰犴犷犸狃狁狎狍狒狨狯狩狲狴狷猁狳猃狺狻猗猓猡猊猞猝猕猢猹猥猬猸猱獐獍獗獠獬獯獾"+
"舛夥飧夤夂饣饧饨饩饪饫饬饴饷饽馀馄馇馊馍馐馑馓馔馕庀庑庋庖庥庠庹庵庾庳赓廒廑廛廨廪膺忄忉忖忏怃忮怄忡忤忾怅怆忪忭忸怙怵怦怛怏怍怩怫怊怿怡恸恹恻恺恂恪恽悖悚悭悝悃悒悌悛惬悻悱惝惘惆惚悴愠愦愕愣惴愀愎愫慊慵憬憔憧憷懔懵忝隳闩闫闱闳闵闶闼闾阃阄阆阈阊阋阌阍阏阒阕阖阗阙阚丬爿戕氵汔汜汊沣沅沐沔沌汨汩汴汶沆沩泐泔沭泷泸泱泗沲泠泖泺泫泮沱泓泯泾洹洧洌浃浈洇洄洙洎洫浍洮洵洚浏浒浔洳涑浯涞涠浞涓涔浜浠浼浣渚淇淅淞渎涿淠渑淦淝淙渖涫渌涮渫湮湎湫溲湟溆湓湔渲渥湄滟溱溘滠漭滢溥溧溽溻溷滗溴滏溏滂"+
"溟潢潆潇漤漕滹漯漶潋潴漪漉漩澉澍澌潸潲潼潺濑濉澧澹澶濂濡濮濞濠濯瀚瀣瀛瀹瀵灏灞宀宄宕宓宥宸甯骞搴寤寮褰寰蹇謇辶迓迕迥迮迤迩迦迳迨逅逄逋逦逑逍逖逡逵逶逭逯遄遑遒遐遨遘遢遛暹遴遽邂邈邃邋彐彗彖彘尻咫屐屙孱屣屦羼弪弩弭艴弼鬻屮妁妃妍妩妪妣妗姊妫妞妤姒妲妯姗妾娅娆姝娈姣姘姹娌娉娲娴娑娣娓婀婧婊婕娼婢婵胬媪媛婷婺媾嫫媲嫒嫔媸嫠嫣嫱嫖嫦嫘嫜嬉嬗嬖嬲嬷孀尕尜孚孥孳孑孓孢驵驷驸驺驿驽骀骁骅骈骊骐骒骓骖骘骛骜骝骟骠骢骣骥骧纟纡纣纥纨纩纭纰纾绀绁绂绉绋绌绐绔绗绛绠绡绨绫绮绯绱绲缍绶绺绻绾缁缂缃"+
"缇缈缋缌缏缑缒缗缙缜缛缟缡缢缣缤缥缦缧缪缫缬缭缯缰缱缲缳缵幺畿巛甾邕玎玑玮玢玟珏珂珑玷玳珀珉珈珥珙顼琊珩珧珞玺珲琏琪瑛琦琥琨琰琮琬琛琚瑁瑜瑗瑕瑙瑷瑭瑾璜璎璀璁璇璋璞璨璩璐璧瓒璺韪韫韬杌杓杞杈杩枥枇杪杳枘枧杵枨枞枭枋杷杼柰栉柘栊柩枰栌柙枵柚枳柝栀柃枸柢栎柁柽栲栳桠桡桎桢桄桤梃栝桕桦桁桧桀栾桊桉栩梵梏桴桷梓桫棂楮棼椟椠棹椤棰椋椁楗棣椐楱椹楠楂楝榄楫榀榘楸椴槌榇榈槎榉楦楣楹榛榧榻榫榭槔榱槁槊槟榕槠榍槿樯槭樗樘橥槲橄樾檠橐橛樵檎橹樽樨橘橼檑檐檩檗檫猷獒殁殂殇殄殒殓殍殚殛殡殪轫轭轱轲轳轵轶"+
"轸轷轹轺轼轾辁辂辄辇辋辍辎辏辘辚軎戋戗戛戟戢戡戥戤戬臧瓯瓴瓿甏甑甓攴旮旯旰昊昙杲昃昕昀炅曷昝昴昱昶昵耆晟晔晁晏晖晡晗晷暄暌暧暝暾曛曜曦曩贲贳贶贻贽赀赅赆赈赉赇赍赕赙觇觊觋觌觎觏觐觑牮犟牝牦牯牾牿犄犋犍犏犒挈挲掰搿擘耄毪毳毽毵毹氅氇氆氍氕氘氙氚氡氩氤氪氲攵敕敫牍牒牖爰虢刖肟肜肓肼朊肽肱肫肭肴肷胧胨胩胪胛胂胄胙胍胗朐胝胫胱胴胭脍脎胲胼朕脒豚脶脞脬脘脲腈腌腓腴腙腚腱腠腩腼腽腭腧塍媵膈膂膑滕膣膪臌朦臊膻臁膦欤欷欹歃歆歙飑飒飓飕飙飚殳彀毂觳斐齑斓於旆旄旃旌旎旒旖炀炜炖炝炻烀炷炫炱烨烊焐焓焖焯焱"+
"煳煜煨煅煲煊煸煺熘熳熵熨熠燠燔燧燹爝爨灬焘煦熹戾戽扃扈扉礻祀祆祉祛祜祓祚祢祗祠祯祧祺禅禊禚禧禳忑忐怼恝恚恧恁恙恣悫愆愍慝憩憝懋懑戆肀聿沓泶淼矶矸砀砉砗砘砑斫砭砜砝砹砺砻砟砼砥砬砣砩硎硭硖硗砦硐硇硌硪碛碓碚碇碜碡碣碲碹碥磔磙磉磬磲礅磴礓礤礞礴龛黹黻黼盱眄眍盹眇眈眚眢眙眭眦眵眸睐睑睇睃睚睨睢睥睿瞍睽瞀瞌瞑瞟瞠瞰瞵瞽町畀畎畋畈畛畲畹疃罘罡罟詈罨罴罱罹羁罾盍盥蠲钅钆钇钋钊钌钍钏钐钔钗钕钚钛钜钣钤钫钪钭钬钯钰钲钴钶钷钸钹钺钼钽钿铄铈铉铊铋铌铍铎铐铑铒铕铖铗铙铘铛铞铟铠铢铤铥铧铨铪铩铫铮铯铳铴铵铷铹铼"+
"铽铿锃锂锆锇锉锊锍锎锏锒锓锔锕锖锘锛锝锞锟锢锪锫锩锬锱锲锴锶锷锸锼锾锿镂锵镄镅镆镉镌镎镏镒镓镔镖镗镘镙镛镞镟镝镡镢镤镥镦镧镨镩镪镫镬镯镱镲镳锺矧矬雉秕秭秣秫稆嵇稃稂稞稔稹稷穑黏馥穰皈皎皓皙皤瓞瓠甬鸠鸢鸨鸩鸪鸫鸬鸲鸱鸶鸸鸷鸹鸺鸾鹁鹂鹄鹆鹇鹈鹉鹋鹌鹎鹑鹕鹗鹚鹛鹜鹞鹣鹦鹧鹨鹩鹪鹫鹬鹱鹭鹳疒疔疖疠疝疬疣疳疴疸痄疱疰痃痂痖痍痣痨痦痤痫痧瘃痱痼痿瘐瘀瘅瘌瘗瘊瘥瘘瘕瘙瘛瘼瘢瘠癀瘭瘰瘿瘵癃瘾瘳癍癞癔癜癖癫癯翊竦穸穹窀窆窈窕窦窠窬窨窭窳衤衩衲衽衿袂裆袷袼裉裢裎裣裥裱褚裼裨裾裰褡褙褓褛褊褴褫褶襁襦疋胥皲皴矜耒"+
"耔耖耜耠耢耥耦耧耩耨耱耋耵聃聆聍聒聩聱覃顸颀颃颉颌颍颏颔颚颛颞颟颡颢颥颦虍虔虬虮虿虺虼虻蚨蚍蚋蚬蚝蚧蚣蚪蚓蚩蚶蛄蚵蛎蚰蚺蚱蚯蛉蛏蚴蛩蛱蛲蛭蛳蛐蜓蛞蛴蛟蛘蛑蜃蜇蛸蜈蜊蜍蜉蜣蜻蜞蜥蜮蜚蜾蝈蜴蜱蜩蜷蜿螂蜢蝽蝾蝻蝠蝰蝌蝮螋蝓蝣蝼蝤蝙蝥螓螯螨蟒蟆螈螅螭螗螃螫蟥螬螵螳蟋蟓螽蟑蟀蟊蟛蟪蟠蟮蠖蠓蟾蠊蠛蠡蠹蠼缶罂罄罅舐竺竽笈笃笄笕笊笫笏筇笸笪笙笮笱笠笥笤笳笾笞筘筚筅筵筌筝筠筮筻筢筲筱箐箦箧箸箬箝箨箅箪箜箢箫箴篑篁篌篝篚篥篦篪簌篾篼簏簖簋簟簪簦簸籁籀臾舁舂舄臬衄舡舢舣舭舯舨舫舸舻舳舴舾艄艉艋艏艚艟艨衾袅袈裘裟襞羝羟"+
"羧羯羰羲籼敉粑粝粜粞粢粲粼粽糁糇糌糍糈糅糗糨艮暨羿翎翕翥翡翦翩翮翳糸絷綦綮繇纛麸麴赳趄趔趑趱赧赭豇豉酊酐酎酏酤酢酡酰酩酯酽酾酲酴酹醌醅醐醍醑醢醣醪醭醮醯醵醴醺豕鹾趸跫踅蹙蹩趵趿趼趺跄跖跗跚跞跎跏跛跆跬跷跸跣跹跻跤踉跽踔踝踟踬踮踣踯踺蹀踹踵踽踱蹉蹁蹂蹑蹒蹊蹰蹶蹼蹯蹴躅躏躔躐躜躞豸貂貊貅貘貔斛觖觞觚觜觥觫觯訾謦靓雩雳雯霆霁霈霏霎霪霭霰霾龀龃龅龆龇龈龉龊龌黾鼋鼍隹隼隽雎雒瞿雠銎銮鋈錾鍪鏊鎏鐾鑫鱿鲂鲅鲆鲇鲈稣鲋鲎鲐鲑鲒鲔鲕鲚鲛鲞鲟鲠鲡鲢鲣鲥鲦鲧鲨鲩鲫鲭鲮鲰鲱鲲鲳鲴鲵鲶鲷鲺鲻鲼鲽鳄鳅鳆鳇鳊鳋鳌鳍鳎鳏鳐鳓鳔"+
"鳕鳗鳘鳙鳜鳝鳟鳢靼鞅鞑鞒鞔鞯鞫鞣鞲鞴骱骰骷鹘骶骺骼髁髀髅髂髋髌髑魅魃魇魉魈魍魑飨餍餮饕饔髟髡髦髯髫髻髭髹鬈鬏鬓鬟鬣麽麾縻麂麇麈麋麒鏖麝麟黛黜黝黠黟黢黩黧黥黪黯鼢鼬鼯鼹鼷鼽鼾齄";
byte[] array = new byte [2];

string return_py ="";
for(int i=0;i<hz.Length ;i++)
{
array = System.Text.Encoding.Default.GetBytes(hz[i].ToString());
if (array[0]<176) //.非汉字
{
return_py += hz[i];
}
else if(array[0]>=176 && array[0]<=215) //一级汉字
{

if (hz[i].ToString ().CompareTo ("匝")>=0)
return_py += "z";
else if (hz[i].ToString ().CompareTo ("压")>=0)
return_py += "y";
else if (hz[i].ToString ().CompareTo ("昔")>=0)
return_py += "x";
else if (hz[i].ToString ().CompareTo ("挖")>=0)
return_py += "w";
else if (hz[i].ToString ().CompareTo ("塌")>=0)
return_py += "t";
else if (hz[i].ToString ().CompareTo ("撒")>=0)
return_py += "s";
else if (hz[i].ToString ().CompareTo ("然")>=0)
return_py += "r";
else if (hz[i].ToString ().CompareTo ("期")>=0)
return_py += "q";
else if (hz[i].ToString ().CompareTo ("啪")>=0)
return_py += "p";
else if (hz[i].ToString ().CompareTo ("哦")>=0)
return_py += "o";
else if (hz[i].ToString ().CompareTo ("拿")>=0)
return_py += "n";
else if (hz[i].ToString ().CompareTo ("妈")>=0)
return_py += "m";
else if (hz[i].ToString ().CompareTo ("垃")>=0)
return_py += "l";
else if (hz[i].ToString ().CompareTo ("喀")>=0)
return_py += "k";
else if (hz[i].ToString ().CompareTo ("击")>=0)
return_py += "j";
else if (hz[i].ToString ().CompareTo ("哈")>=0)
return_py += "h";
else if (hz[i].ToString ().CompareTo ("噶")>=0)
return_py += "g";
else if (hz[i].ToString ().CompareTo ("发")>=0)
return_py += "f";
else if (hz[i].ToString ().CompareTo ("蛾")>=0)
return_py += "e";
else if (hz[i].ToString ().CompareTo ("搭")>=0)
return_py += "d";
else if (hz[i].ToString ().CompareTo ("擦")>=0)
return_py += "c";
else if (hz[i].ToString ().CompareTo ("芭")>=0)
return_py += "b";
else if (hz[i].ToString ().CompareTo ("啊")>=0)
return_py += "a";
}
else if(array[0]>=215) //二级汉字
{
return_py += ls_second_eng.Substring (ls_second_ch.IndexOf (hz[i].ToString (),0),1);
}
}
return return_py.ToUpper();
}

 

拼音、汉字转换

这个太简单,你说的文章只能解决一级字库问题,而且还牵扯到多音字,我们用拼音首字母的目的主要是解决姓名等的简写问题,因此,我们可以将所有国标汉字的拼音首字母取出来,放到一个表中,然后去查就可以了,那么如何取呢?也很简单,也windows 98等自带的输入法生成器,逆转化一下拼音输入法的码表,然后借助foxpro等的工具就可以了,最后将一些特殊字的拼音首字母改一下,如"仇"由"c"改"q",因为做姓时"仇"读qiou,同样,要修改单等,这样,一次生成,终生受用,如果你还嫌麻烦,我把我转换好的用excel文件给你,放在http://www.lqjt.com/data/py.rar中.
我根据传来的姓名生成简写的函数如下(如输入"张三",返回"zs")

/// <summary>
/// 根据姓名自动生成简写
/// </summary>
/// <param name="strName">中文名称</param>
/// <returns>该中文名称的拼音首字母组合</returns>
public static string GetSimpleName(string strName)
{
string strSimple="";
strName=strName.Trim();
DataSet myDataSet=new DataSet();
if(strName.Length>0)
{
string strSql="select * from 拼音首字母";
string strWhere=" where 汉字='"+strName[0].ToString()+"'";
for(int i=1;i<strName.Length;i++)
{
strWhere+=" or 汉字='"+strName[i].ToString()+"'";
}
strSql+=strWhere;

ArrayList arrayResult=new ArrayList(),arrayField=new ArrayList();
arrayField.Add("汉字");
arrayField.Add("首字母");

if(Tool.FillReaderInfo(strSql,arrayField,arrayResult,"拼音首字母",Tool.g_CharSplit) )

{
//分解姓名
for(int i=0;i<strName.Length;i++)
{
char word=strName[i];
//从拼音表中找到该字
foreach(string s in arrayResult)
{
if(s[0]==word)//找到该字
{
strSimple+=s[2].ToString();
break;
}
}//foreach
}
}
}
return strSimple;
}

 


 

字典解决方案

TextBox tbX=new TextBox();
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
tbX.Text=(string)listBox1.SelectedItem;
}

private void textBox1_Enter(object sender, System.EventArgs e)
{
tbX=textBox1;
this.listBox1.Items.Clear();
this.listBox1.Items.AddRange(new object[] {
"tb1",
"along",
"yanzi",
"laopo"});
}

private void textBox2_Enter(object sender, System.EventArgs e)
{
tbX=textBox2;
this.listBox1.Items.Clear();
this.listBox1.Items.AddRange(new object[] {
"文本2",
"阿龙",
"燕子",
"老婆"});
}

 

XML & XSL

SQL存储过程:最后一句写:FOR XML AUTO

FORM:

SqlCommand cm=new SqlCommand("cheTopXML",cn);
cm.CommandType=CommandType.StoredProcedure;
cn.Open();
StringBuilder StrBuild=new StringBuilder();
SqlDataReader da2=cm.ExecuteReader();
while(da2.Read())
{
StrBuild.Append(da2.GetString(0));
}
label1.Text= "<?xml version='1.0' ?><?xml:stylesheet type='text/xsl' href='XSLTFile2.xslt' ?><roota>"+StrBuild.ToString()+"</roota>";

保存:
XmlDocument xmldoc3=new XmlDocument();
/*FileStream FS2=new FileStream("my.xml",FileMode.Create);
StreamWriter sw2=new StreamWriter(FS2);
sw2.Write(label1.Text);*/
xmldoc3.Load("my.xml");//XmlDocment对象怎样才能load()本就在内存中的含有XML内容的string对象?
XPathNavigator xPNav1=xmldoc3.CreateNavigator();
XslTransform xslTr=new XslTransform();
xslTr.Load("..//..//XSLTFile2.xslt");
StringWriter output2=new StringWriter();
xslTr.Transform(xPNav1,null,output2);
FileStream FS=new FileStream("myXML.html",FileMode.Create);
StreamWriter sw=new StreamWriter(FS);
sw.Write(output2.ToString());
sw.Close();
output2.Close();

XSL:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--<xsl:output method = "xml" omit-xml-declaration = "no" doctype-system = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype-public = "-//W3c//DTD XHTML 1.0 Strict//EN" encoding="GB2312" />-->
<xsl:template match = "/"><!--注意:此后如果跟match="*",就将乱码 -->
<html >
<head><title>阿龙的第一个XML页</title></head>
<body><h1>阿龙的第一个XML页</h1>
<p><b>我是阿龙:</b>
<b><xsl:value-of select="*/che1/chezhu" /></b>
</p></body>
<xsl:apply-templates select="roota"/><!--注意:此后不能跟match="" -->
</html>
</xsl:template>

<xsl:template match="roota" >
<table border="3" style='width:600'><tr><th>id</th><th>单位</th><th>车主</th></tr>
<xsl:for-each select="che1">
<tr><td><xsl:value-of select="id" /></td><td><xsl:value-of select="danwei"/></td><td><xsl:value-of select="chezhu"/></td></tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

 

类或用户控件引用页面中的控件:

类:
using System;
using System.Web;
using System.Web.UI; //必须要
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using System.Web.UI.WebControls;
namespace ooa.MyHtml
{
public class xx
{
protected HttpResponse Response;//重要
protected Label la3;//重要
public xx(object parent) //在页面中,此parent参数一般为this
{
//
// TODO: 在此处添加构造函数逻辑
//
la3 = ((System.Web.UI.Page)parent).FindControl("Label1") as Label;//最重要!!
//用户控件中表现为:this.Page.FindControl("Label3") as Label;
Response = ((System.Web.UI.Page)parent).Response;
}
public void aa()
{
if (la3.Visible==false)
{
Response.Write("<script language=javascript>alert('请用正版')</script>");
}
else
{Response.Write("<script language=javascript>function gg(){alert('sdkf');}</script>");
}
}
}
}

 

Cookie:

//创建一个HttpCookie对象
HttpCookie cookie = new HttpCookie ( NameField . Text ) ;
//设定此cookies值
cookie . Value = ValueField . Text ;
//设定cookie的生命周期,在这里定义为一个小时
DateTime dtNow = DateTime . Now ;
TimeSpan tsMinute = new TimeSpan ( 0 , 1 , 0 , 0 ) ;
cookie . Expires = dtNow + tsMinute ;
cookie [ "姓名" ] = "王天" ;
cookie [ "性别" ] = "男" ;
cookie [ "年龄" ] = "26" ;
//加入此cookie
Response . Cookies . Add ( cookie ) ;
Response . Write ( NameField . Text + "Cookie创建完毕 <br > <hr > " ) ;

//获得cookie
HttpCookie cookie = Request.Cookies [ strCookieName ] ;
//确定是否存在用户输入的cookie
if ( null == cookie ) {
Response.Write ( "没有发现指定的cookie <br > <hr > " ) ;
}
else {
//找到指定的cookie,显示cookie的值
String strCookieValue = cookie . Value . ToString ( ) ;
Response.Write ( strCookieName + " cookie 的值为: <b > "
+ strCookieValue + " </b > <br > <hr > " ) ;

Session:

Session["myname"]="along"; Session["myname"].Remove();

 

iframe的绑定:

protected System.Web.UI.HtmlControls.HtmlInputHidden hneirong;
string b1="iframe1.document.designMode='on';iframe1.document.body.innerHTML=/'";
string b2="/';";

string b3;
if(!IsPostBack)
{
Button1.Attributes["onmouseover"]="hneirong.value=iframe1.document.body.innerHTML;";
iframe1.Attributes["onload"]="iframe1.document.designMode='on';";
if(Session["neirong"]==null)
{
iframe1.Attributes["onload"]+="iframe1.document.body.innerHTML='<i>这是初始内容</i>';";
}
else
{
iframe1.Attributes["onload"]+=b1+Session["neirong"].ToString()+b2;
}
}
else
{
iframe1.Attributes["onload"]=b1+b3+b2;
}

private void Button1_Click(object sender, System.EventArgs e)
{
b3= hneirong.Value.Replace("/r/n","");
Session["neirong"]=b3;
Response.Redirect("yulan.aspx");
}

 

Split、Substring、Replace的综合用法:

string bb="中国人, 历史, 前途, 第四, opq, rst, uvw, xyz";
string cc="";
cc=bb.Replace(", ",",");
string[] renshushu=cc.Split(',');
int renshu=renshushu.Length;
// for (loop=0; loop < friend_array.length; loop++)
string zh="xyz";
int inof=cc.IndexOf(zh);
string zuo="",you="",he="";
if (inof!=-1)
{
if(inof==0)
{ he=cc.Substring(zh.Length+1);}
else if(inof+zh.Length==cc.Length)
{he=cc.Substring(0,inof-1);}
else
{
zuo=cc.Substring(0,inof);
you=cc.Substring(inof+zh.Length+1);
he=String.Concat(zuo,you).ToString();
}
}
Label3.Text="renshu:"+renshu+"<br>"+zuo+"<br>"+you+"<br>"+he;

 

数据库连接、CheckListBox的绑定

string strConn=ConfigurationSettings.AppSettings["cnstr"];
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select wid,wenjianming from hongtoud",cn);
dataSet1=new DataSet();
da.Fill(dataSet1,"ht");
CheckBoxList1.DataSource=dataSet1.Tables["ht"].DefaultView;
CheckBoxList1.DataTextField="wenjianming";
CheckBoxList1.DataValueField="wid";
SqlCommand cm2=new SqlCommand("select wid,yonghu from usertab where uid=3",cn);
SqlDataReader dr2=cm2.ExecuteReader();
dr2.Read();
string yonghuwid=dr2["wid"].ToString();
string[] yonghuwidArr=yonghuwid.Split(',');
int shu=yonghuwidArr.Length;
CheckBoxList1.DataBind();
for(int i=0;i<shu;i++)
{
foreach (ListItem li in CheckBoxList1.Items)
{if(li.Value.ToString()==yonghuwidArr[i])
{
li.Selected=true;
}
}
}

 

页面间传递值,用SESSION最好,页内传值,用hiddenInput最好,因为一般的变量在刷新后会没有了。

 

0 0

相关博文

我的热门文章

img
取 消
img