阳光地带

风雨雷电

博客园 首页 新随笔 联系 订阅 管理
  17 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

2007年5月17日 #

发个select语句的练习,希望对初学者有帮助,答案我会在后面发出来
CREATE TABLE STUDENT
(SNO VARCHAR(3) NOT NULL,
SNAME VARCHAR(4) NOT NULL,
SSEX VARCHAR(2) NOT NULL,
SBIRTHDAY DATETIME,
CLASS VARCHAR(5))
go
CREATE TABLE COURSE
(CNO VARCHAR(5) NOT NULL,
CNAME VARCHAR(10) NOT NULL,
TNO VARCHAR(10) NOT NULL)
go
CREATE TABLE SCORE
(SNO VARCHAR(3) NOT NULL,
CNO VARCHAR(5) NOT NULL,
DEGREE NUMERIC(10, 1) NOT NULL)
go
CREATE TABLE TEACHER
(TNO VARCHAR(3) NOT NULL,
TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL,
TBIRTHDAY DATETIME NOT NULL, PROF VARCHAR(6),
DEPART VARCHAR(10) NOT NULL)

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,1977-09-01,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,1975-10-02,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,1976-01-23,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,1976-02-20,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,1975-02-10,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,1974-06-03,95031);
GO
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825)
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);
GO
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
GO
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');



1、 查询Student表中的所有记录的Sname、Ssex和Class列。
2、 查询教师所有的单位即不重复的Depart列。
3、 查询Student表的所有记录。
4、 查询Score表中成绩在60到80之间的所有记录。
5、 查询Score表中成绩为85,86或88的记录。
6、 查询Student表中“95031”班或性别为“女”的同学记录。
7、 以Class降序查询Student表的所有记录。
8、 以Cno升序、Degree降序查询Score表的所有记录。
9、 查询“95031”班的学生人数。
10、查询Score表中的最高分的学生学号和课程号。
11、查询‘3-105’号课程的平均分。
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
13、查询最低分大于70,最高分小于90的Sno列。
14、查询所有学生的Sname、Cno和Degree列。
15、查询所有学生的Sno、Cname和Degree列。
16、查询所有学生的Sname、Cname和Degree列。
17、查询“95033”班所选课程的平均分。
18、假设使用如下命令建立了一个grade表:
create table grade(low number(3,0),upp number(3),rank char(1));
insert into grade values(90,100,’A’);
insert into grade values(80,89,’B’);
insert into grade values(70,79,’C’);
insert into grade values(60,69,’D’);
insert into grade values(0,59,’E’);
commit;
现查询所有同学的Sno、Cno和rank列。
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
23、查询“张旭“教师任课的学生成绩。
24、查询选修某课程的同学人数多于5人的教师姓名。
25、查询95033班和95031班全体学生的记录。
26、查询存在有85分以上成绩的课程Cno.
27、查询出“计算机系“教师所教课程的成绩表。
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
31、查询所有教师和同学的name、sex和birthday.
32、查询所有“女”教师和“女”同学的name、sex和birthday.
33、查询成绩比该课程平均成绩低的同学的成绩表。
34、查询所有任课教师的Tname和Depart.
35 查询所有未讲课的教师的Tname和Depart.
36、查询至少有2名男生的班号。
37、查询Student表中不姓“王”的同学记录。
38、查询Student表中每个学生的姓名和年龄。
39、查询Student表中最大和最小的Sbirthday日期值。
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
41、查询“男”教师及其所上的课程。
42、查询最高分同学的Sno、Cno和Degree列。
43、查询和“李军”同性别的所有同学的Sname.
44、查询和“李军”同性别并同班的同学Sname.
45、查询所有选修“计算机导论”课程的“男”同学的成绩表

不好意思哈,时间太长给忘记了,下面是参考答案:
SQL语句练习题参考答案
1、 select Sname,Ssex,Class from Student;
2、 select distinct depart from teacher;
3、 select Sno as '学号',Sname as '姓名',Ssex as '性别',Sbirthday as'出生日期',Class as'班号'from student;

select Sno as 学号,Sname as 姓名,Ssex as 性别,Sbirthday as 出生日期,Class as 班号 from student;
4、 select * from score where degree between 60 and 80;
或select * from score where degree>=60 and degree<=80;
5、 select * from score where degree in (85,86,88);
6、 select * from student where class='95031'or Ssex='女';
7、 select * from student order by class desc;
8、 select * from score order by cno asc ,degree desc;
或select * from score order by cno ,degree desc;
9、 select count(*) as CNT from student where class='95031';
10、select Sno as '学号',cno as '课程号', degree as '最高分' from score
where degree=(select max(degree) from score)
11、select avg(degree)as 课程平均分 from score where cno='3-105';
12、select cno,avg(degree) from score where cno like'3%'group by cno having count(*) >5;
13、select Sno from score group by Sno having min(degree)>70 and max(degree)<90;
14、select student.Sname,score.Cno,score.degree from student,score where student.Sno=score.Sno;
15、select x.Sno,y.Cname,x.degree from score x,course y where x.Cno=y.Cno;
16、select x.Sname,y.Cname,z.degree from student x,course y,score z where x.Sno=z.Sno and z.Cno=y.Cno;
17、select y.Cno,avg(y.degree) from student x,score y where x.Sno=y.Sno and x.class='95033'group by y.cno;
18、select Sno,Cno,rank from score,grade where degree between low and upp order by rank;
19、select x.Cno,x.Sno,x.degree from score x,score y
where x.cno='3-105' and x.degree>y.degree and y.sno='109'and y.cno='3-105';
20、
1,查询成绩非本科最高 select * from score b where degree <(select max(degree) from score a where a.cno=b.cno);
2,查询成绩非本科最高并且选2门以上的学生的成绩:
21、select x.cno,x.Sno,x.degree from score x,score y where x.degree>y.degree and y.sno='109'and y.cno='3-105';
select cno,sno,degree from score where degree >(select degree from score where sno='109' and cno='3-105')
22、select sno,sname,sbirthday from student where to_char(sbirthday,'yyyy')=(select to_char(sbirthday,'yyyy') from student where sno='108');
23、select cno,sno,degree from score where cno=(select x.cno from course x,teacher y where x.tno=y.tno and y.tname='张旭');
24、select tname from teacher where tno in(select x.tno from course x,score y where x.cno=y.cno group by x.tno having count(x.tno)>5);
25、select * from student where class in('95033','95031');
26、select distinct cno from score where degree in (select degree from score where degree>85);
27、select * from score where cno in(select x.cno from course x,teacher y where y.tno=x.tno and y.depart='计算机系');
28、select tname,prof from teacher where depart='计算机系' and prof not in (select prof from teacher where depart='电子工程系');
29、select * from score where cno='3-105' and degree>any (select degree from score where cno='3-245')order by degree desc;
30、select * from score where cno='3-105' and degree>all(select degree from score where cno='3-245');
31、select tname,tsex,tbirthday from teacher
union
select sname,ssex,sbirthday from student;
32、select tname,tsex,tbirthday from teacher where tsex='女'
union
select sname,ssex,sbirthday from student where ssex='女';
33、select * from score a where degree<(select avg(degree)
from score b where a.cno=b.cno);
34、select tname,depart from teacher a where exists
(select * from course b where a.tno=b.tno);
35、select tname,depart from teacher a where not exists
(select * from course b where a.tno=b.tno);
36、select class from student where ssex='男'group by class having count(*)>=2;
37、select * from student where sname not like'王_';
38、select sname as 姓名,(to_char(sysdate,'yyyy')-to_char(sbirthday,'yyyy')) as 年龄 from student
39、select sname,sbirthday as 最大 from student where sbirthday =(select min (sbirthday) from student)
union
select sname,sbirthday as 最小 from student where sbirthday =(select max(sbirthday) from student)
40、select class,sname,sbirthday from student order by class desc,sbirthday;
41、select x.tname,y.cname from teacher x,course y where x.tno=y.tno and x.tsex='男';
42、select * from score where degree=(select max(degree)from score);
43、select sname from student where ssex=(select ssex from student where sname='李军');
44、select sname from student where ssex=(select ssex from student where sname='李军') and class=(select class from student where sname='李军');
45、select * from score where sno in(select sno from student where ssex='男') and cno=(select cno from course
where cname='计算机导论');
posted @ 2007-05-17 10:30 阳光地带 阅读(1954) 评论(4) 编辑

  1. truncate table 表名
  2. delete from tablename

TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少。

DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。

TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。

对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。

TRUNCATE TABLE 不能用于参与了索引视图的表

posted @ 2007-05-17 10:08 阳光地带 阅读(486) 评论(0) 编辑

2007年5月10日 #

#region 把AD域中得所有用户添加到下拉列表中
    public void DataDropDown()
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://HPI.COM.CN"); //连接AD域
        System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
        //定义查询Seacher
        mySearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
        //过滤从User组中得到人名
        mySearcher.PropertiesToLoad.Add("name");//添加中文用户名
        mySearcher.PropertiesToLoad.Add("samaccountname");//添加登陆名
        SearchResultCollection resultCol = mySearcher.FindAll();//从AD中全部查询到内存中

        foreach (SearchResult result in resultCol)
        {
            ResultPropertyCollection props = result.Properties;
            string name = "";
            string accountname = "";
           foreach (string propName in props.PropertyNames)
            {
                bool flag = false;
                if (propName == "name")
                {
                    name = props[propName][0].ToString();
                }
                if (propName == "samaccountname")
                {
                    accountname = props[propName][0].ToString();
                    flag = true;
                }
            }
            DropDownList1.Items.Add(new ListItem(name, accountname));//将数据集潜入到下拉列表中
        }

    }
    #endregion

posted @ 2007-05-10 16:17 阳光地带 阅读(242) 评论(0) 编辑

2007年4月29日 #

摘要: 这个存储过程的作用是自动生成编号,比如我们在数据库中有个**票编号字段,该存储过程可以生成格式为头(自己定义的一个头,比如AA)+日期(形如20070401)+6位递增整数(形如002254),并且每到了新的一年后面的6位整数便会归0重新开始。其中用到了动态执行sql的方法。参数有四个,需要给出表名称,字段名称,头和一个返回值。===================================...阅读全文
posted @ 2007-04-29 15:26 阳光地带 阅读(214) 评论(0) 编辑

摘要: 把长日期转换为短日期 Convert(char(10),getdate(),120) MS-SQL数据库开发常用汇总1.按姓氏笔画排序:Select*FromTableNameOrderByCustomerNameCollateChinese_PRC_Stroke_ci_as2.数据库加密:selectencrypt('原始密码')selectpwdencrypt('原始密码')selectpwd...阅读全文
posted @ 2007-04-29 15:23 阳光地带 阅读(220) 评论(0) 编辑

摘要: 今天做个行列转换 找到的好语句 解决了我的问题 *说明:复制表(只复制结构,源表名:a新表名:b)select*intobfromawhere1<>1*说明:拷贝表(拷贝数据,源表名:a目标表名:b)insertintob(a,b,c)selectd,e,ffromb;*说明:显示文章、提交人和最后回复时间selecta.title,a.username,b.adddatefromta...阅读全文
posted @ 2007-04-29 15:19 阳光地带 阅读(304) 评论(0) 编辑

摘要: /**////<summary>///清空指定页面上所有的控件内容,包括TextBox,CheckBox,CheckBoxList,RadioButton,RadioButtonList。但是不清///除如ListBox,DropDownList,因为这样的控件值对当前页面来说还可以用,一般这些控件里都是保存的字典数据。///Author:Kevin///日期:2004-12-02//...阅读全文
posted @ 2007-04-29 15:14 阳光地带 阅读(315) 评论(0) 编辑

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Mis.Pages
posted @ 2007-04-29 15:11 阳光地带 阅读(483) 评论(1) 编辑

摘要: C#中Split分隔字符串的应用1、用字符串分隔:usingSystem.Text.RegularExpressions;stringstr="aaajsbbbjsccc";string[]sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);foreach(stringiinsArray)Response.Write(i.ToString()+...阅读全文
posted @ 2007-04-29 15:10 阳光地带 阅读(1507) 评论(0) 编辑

C# 出来也有些日子了,最近由于编程的需要,对 C# 的类型转换做了一些研究,其内容涉及 C# 的装箱/拆箱/别名、数值类型间相互转换、字符的 ASCII 码和 Unicode 码、数值字符串和数值之间的转换、字符串和字符数组/字节数组之间的转换、各种数值类型和字节数组之间的转换、十六进制数输出以及日期型数据的一些转换处理,在这里与大家分享——

1. 装箱、拆箱还是别名

  许多 C#.NET 的书上都有介绍 int -> Int32 是一个装箱的过程,反之则是拆箱的过程。许多其它变量类型也是如此,如:short <-> Int16,long <-> Int64 等。对于一般的程序员来说,大可不必去了解这一过程,因为这些装箱和拆箱的动作都是可以自动完成的,不需要写代码进行干预。但是我们需要记住这些类型之间的关系,所以,我们使用“别名”来记忆它们之间的关系。
C# 是全面向对象的语言,比 Java 的面向对象都还彻底——它把简单数据类型通过默认的装箱动作封装成了类。Int32、Int16、Int64 等就是相应的类名,而那些我们熟悉的、简单易记的名称,如 int、short、long 等,我们就可以把它称作是 Int32、Int16、Int64 等类型的别名。
  那么除了这三种类型之外,还有哪些类有“别名”呢?常用的有如下一些:

bool -> System.Boolean (布尔型,其值为 true 或者 false)
char -> System.Char (字符型,占有两个字节,表示 1 个 Unicode 字符)
byte -> System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255)
sbyte -> System.SByte (带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127)
ushort -> System.UInt16 (无符号短整型,占 2 字节,表示 16 位正整数,范围 0 ~ 65,535)
uint -> System.UInt32 (无符号整型,占 4 字节,表示 32 位正整数,范围 0 ~ 4,294,967,295)
ulong -> System.UInt64 (无符号长整型,占 8 字节,表示 64 位正整数,范围 0 ~ 大约 10 的 20 次方)
short -> System.Int16 (短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767)
int -> System.Int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
long -> System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方)
float -> System.Single (单精度浮点型,占 4 个字节)
double -> System.Double (双精度浮点型,占 8 个字节)

  我们可以用下列代码做一个实验:

private void TestAlias() {
    // this.textBox1 是一个文本框,类型为 System.Windows.Forms.TextBox
    // 设计中已经将其 Multiline 属性设置为 true
    byte a = 1; char b = 'a'; short c = 1;
    int d = 2; long e = 3; uint f = 4; bool g = true;
    this.textBox1.Text = "";
    this.textBox1.AppendText("byte -> " + a.GetType().FullName + "\n");
    this.textBox1.AppendText("char -> " + b.GetType().FullName + "\n");
    this.textBox1.AppendText("short -> " + c.GetType().FullName + "\n");
    this.textBox1.AppendText("int -> " + d.GetType().FullName + "\n");
    this.textBox1.AppendText("long -> " + e.GetType().FullName + "\n");
    this.textBox1.AppendText("uint -> " + f.GetType().FullName + "\n");
    this.textBox1.AppendText("bool -> " + g.GetType().FullName + "\n");
}

  在窗体中新建一个按钮,并在它的单击事件中调用该 TestAlias() 函数,我们将看到运行结果如下:

byte -> System.Byte
char -> System.Char
short -> System.Int16
int -> System.Int32
long -> System.Int64
uint -> System.UInt32
bool -> System.Boolean

  这足以说明各别名对应的类!

2. 数值类型之间的相互转换

  这里所说的数值类型包括 byte, short, int, long, fload, double 等,根据这个排列顺序,各种类型的值依次可以向后自动进行转换。举个例来说,把一个 short 型的数据赋值给一个 int 型的变量,short 值会自动行转换成 int 型值,再赋给 int 型变量。如下例:

private void TestBasic() {
    byte a = 1; short b = a; int c = b;
    long d = c; float e = d; double f = e;
    this.textBox1.Text = "";
    this.textBox1.AppendText("byte a = " + a.ToString() + "\n");
    this.textBox1.AppendText("short b = " + b.ToString() + "\n");
    this.textBox1.AppendText("int c = " + c.ToString() + "\n");
    this.textBox1.AppendText("long d = " + d.ToString() + "\n");
    this.textBox1.AppendText("float e = " + e.ToString() + "\n");
    this.textBox1.AppendText("double f = " + f.ToString() + "\n");
}

  译顺利通过,运行结果是各变量的值均为 1;当然,它们的类型分别还是 System.Byte 型……System.Double 型。现在我们来试试,如果把赋值的顺序反过来会怎么样呢?在 TestBasic() 函数中追加如下语句:

int g = 1;
short h = g;
this.textBox1.AppendText("h = " + h.ToString() + "\n");

  结果编译报错:
  G:\Projects\Visual C#\Convert\Form1.cs(118): 无法将类型“int”隐式转换为“short”
  其中,Form1.cs 的 118 行即 short h = g 所在行。

  这个时候,如果我们坚持要进行转换,就应该使用强制类型转换,这在 C 语言中常有提及,就是使用“(类型名) 变量名”形式的语句来对数据进行强制转换。如上例修改如下:

short g = 1;
byte h = (byte) g; // 将 short 型的 g 的值强制转换成 short 型后再赋给变量 h
this.textBox1.AppendText("h = " + h.ToString() + "\n");

  编译通过,运行结果输出了 h = 1,转换成功。
  但是,如果我们使用强制转换,就不得不再考虑一个问题:short 型的范围是 -32768 ~ 23767,而 byte 型的范围是 0 ~ 255,那么,如果变量 g 的大小超过了 byte 型的范围又会出现什么样的情况呢?我们不妨再一次改写代码,将值改为 265,比 255 大 10

short g = 265; //265 = 255 + 10
byte h = (byte) g;
this.textBox1.AppendText("h = " + h.ToString() + "\n");

  编译没有出错,运行结果却不是 h = 265,而是 h = 9。
因此,我们在进行转换的时候,应当注意被转换的数据不能超出目标类型的范围。这不仅体现在多字节数据类型(相对,如上例的 short) 转换为少字节类型(相对,如上例的 byte) 时,也体现在字节数相同的有符号类型和无符号类型之间,如将 byte 的 129 转换为 sbyte 就会溢出。这方面的例子大同小异,就不详细说明了。

3. 字符的 ASCII 码和 Unicode 码

  很多时候我们需要得到一个英文字符的 ASCII 码,或者一个汉字字符的 Unicode 码,或者从相关的编码查询它是哪一个字符的编码。很多人,尤其是从 VB 程序序转过来学 C# 的人,会报怨 C# 里为什么没有提供现成的函数来做这个事情——因为在 VB 中有 Asc() 函数和 Chr() 函数用于这类转换。
但是如果你学过 C,你就会清楚,我们只需要将英文字符型数据强制转换成合适的数值型数据,就可以得到相应的 ASCII 码;反之,如果将一个合适的数值型数据强制转换成字符型数据,就可以得到相应的字符。
C# 中字符的范围扩大了,不仅包含了单字节字符,也可以包含双字节字符,如中文字符等。而在字符和编码之间的转换,则仍延用了 C 语言的做法——强制转换。不妨看看下面的例子

private void TestChar() {
    char ch = 'a'; short ii = 65;
    this.textBox1.Text = "";
    this.textBox1.AppendText("The ASCII code of \'" + ch + "\' is: " + (short) ch + "\n");
    this.textBox1.AppendText("ASCII is " + ii.ToString() + ", the char is: " + (char) ii + "\n");
    char cn = '中'; short uc = 22478;
    this.textBox1.AppendText("The Unicode of \'" + cn + "\' is: " + (short) cn + "\n");
    this.textBox1.AppendText("Unicode is " + uc.ToString() + ", the char is: " + (char) uc + "\n");
}

  它的运行结果是

The ASCII code of 'a' is: 97
ASCII is 65, the char is: A
The Unicode of '中' is: 20013
Unicode is 22478, the char is: 城

  从这个例子中,我们便能非常清楚的了解——通过强制转换,可以得以字符的编码,或者得到编码表示的字符。如果你需要的不是 short 型的编码,请参考第 1 条进行转换,即可得到 int 等类型的编码值。

4. 数值字符串和数值之间的转换

  首先,我们得搞明白,什么是数值字符串。我们知道,在 C# 中,字符串是用一对双引号包含的若干字符来表示的,如 "123"。而 "123" 又相对特殊,因为组成该字符串的字符都是数字,这样的字符串,就是数值字符串。在我们的眼中,这即是一串字符,也是一个数,但计算机却只认为它是一个字符串,不是数。因此,我们在某些时候,比如输入数值的时候,把字符串转换成数值;而在另一些时候,我们需要相反的转换。
  将数值转换成字符串非常简单,因为每一个类都有一个 void ToString() 方法。所有数值型的 void ToString() 方法都能将数据转换为数值字符串。如 123.ToSting() 就将得到字符串 "123"。
那么反过来,将数值型字符串转换成数值又该怎么办呢?我们仔细查找一下,会发现 short, int, float 等数值类型均有一个 static Parse() 函数。这个函数就是用来将字符串转换为相应数值的。我们以一个 float 类型的转换为例: float f = float.Parse("543.21"); 其结果 f 的值为 543.21F。当然,其它的数值类型也可以使用同样的方法进行转换,下面的例子可以更明确的说明转换的方法:

private void TestStringValue() {
    float f = 54.321F;
    string str = "123";
    this.textBox1.Text = "";
    this.textBox1.AppendText("f = " + f.ToString() + "\n");
    if (int.Parse(str) == 123) {
        this.textBox1.AppendText("str convert to int successfully.");
    } else {
        this.textBox1.AppendText("str convert to int failed.");
    }
}

  运行结果:

f = 54.321
str convert to int successfully.

5. 字符串和字符数组之间的转换

  字符串类 System.String 提供了一个 void ToCharArray() 方法,该方法可以实现字符串到字符数组的转换。如下例:

private void TestStringChars() {
    string str = "mytest";
    char[] chars = str.ToCharArray();
    this.textBox1.Text = "";
    this.textBox1.AppendText("Length of \"mytest\" is " + str.Length + "\n");
    this.textBox1.AppendText("Length of char array is " + chars.Length + "\n");
    this.textBox1.AppendText("char[2] = " + chars[2] + "\n");
}

  例中以对转换转换到的字符数组长度和它的一个元素进行了测试,结果如下:

Length of "mytest" is 6
Length of char array is 6
char[2] = t

  可以看出,结果完全正确,这说明转换成功。那么反过来,要把字符数组转换成字符串又该如何呢?
  我们可以使用 System.String 类的构造函数来解决这个问题。System.String 类有两个构造函数是通过字符数组来构造的,即 String(char[]) 和 String[char[], int, int)。后者之所以多两个参数,是因为可以指定用字符数组中的哪一部分来构造字符串。而前者则是用字符数组的全部元素来构造字符串。我们以前者为例,在 TestStringChars() 函数中输入如下语句:

char[] tcs = {'t', 'e', 's', 't', ' ', 'm', 'e'};
string tstr = new String(tcs);
this.textBox1.AppendText("tstr = \"" + tstr + "\"\n");

  运行结果输入 tstr = "test me",测试说明转换成功。
  实际上,我们在很多时候需要把字符串转换成字符数组只是为了得到该字符串中的某个字符。如果只是为了这个目的,那大可不必兴师动众的去进行转换,我们只需要使用 System.String 的 [] 运算符就可以达到目的。请看下例,再在 TestStringChars() 函数中加入如如下语名:

char ch = tstr[3];
this.textBox1.AppendText("\"" + tstr + "\"[3] = " + ch.ToString());

  正确的输出是 "test me"[3] = t,经测试,输出正确。

6. 字符串和字节数组之间的转换

  如果还想从 System.String 类中找到方法进行字符串和字节数组之间的转换,恐怕你会失望了。为了进行这样的转换,我们不得不借助另一个类:System.Text.Encoding。该类提供了 bye[] GetBytes(string) 方法将字符串转换成字节数组,还提供了 string GetString(byte[]) 方法将字节数组转换成字符串。
  System.Text.Encoding 类似乎没有可用的构造函数,但我们可以找到几个默认的 Encoding,即 Encoding.Default(获取系统的当前 ANSI 代码页的编码)、Encoding.ASCII(获取 7 位 ASCII 字符集的编码)、Encoding.Unicode(获取采用 Little-Endian 字节顺序的 Unicode 格式的编码)、Encoding.UTF7(获取 UTF-7 格式的编码)、Encoding.UTF8(获取 UTF-8 格式的编码) 等。这里主要说说 Encoding.Default 和 Encoding.Unicode 用于转换的区别。
  在字符串转换到字节数组的过程中,Encoding.Default 会将每个单字节字符,如半角英文,转换成 1 个字节,而把每个双字节字符,如汉字,转换成 2 个字节。而 Encoding.Unicode 则会将它们都转换成两个字节。我们可以通过下列简单的了解一下转换的方法,以及使用 Encoding.Default 和 Encodeing.Unicode 的区别:

private void TestStringBytes() {
    string s = "C#语言";
    byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
    byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s);
    string t1 = "", t2 = "";
    foreach (byte b in b1) {
        t1 += b.ToString("") + " ";
    }
    foreach (byte b in b2) {
        t2 += b.ToString("") + " ";
    }
    this.textBox1.Text = "";
    this.textBox1.AppendText("b1.Length = " + b1.Length + "\n");
    this.textBox1.AppendText(t1 + "\n");
    this.textBox1.AppendText("b2.Length = " + b2.Length + "\n");
    this.textBox1.AppendText(t2 + "\n");
}

  运行结果如下,不说详述,相信大家已经明白了。

b1.Length = 6
67 35 211 239 209 212
b2.Length = 8
67 0 35 0 237 139 0 138

  将字节数组转换成字符串,使用 Encoding 类的 string GetString(byte[]) 或 string GetString(byte[], int, int) 方法,具体使用何种 Encoding 还是由编码决定。在 TestStringBytes() 函数中添加如下语句作为实例:

byte[] bs = {97, 98, 99, 100, 101, 102};
string ss = System.Text.Encoding.ASCII.GetString(bs);
this.textBox1.AppendText("The string is: " + ss + "\n");

  运行结果为:The string is: abcdef

7. 各种数值类型和字节数组之间的转换

  在第 1 条中我们可以查到各种数值型需要使用多少字节的空间来保存数据。将某种数值类型的数据转换成字节数组的时候,得到的一定是相应大小的字节数组;同样,需要把字节数组转换成数值类型,也需要这个字节数组大于相应数值类型的字节数。
  现在介绍此类转换的主角:System.BitConverter。该类提供了 byte[] GetBytes(...) 方法将各种数值类型转换成字节数组,也提供了 ToInt32、ToInt16、ToInt64、ToUInt32、ToSignle、ToBoolean 等方法将字节数组转换成相应的数值类型。

  由于这类转换通常只是在需要进行较细微的编码/解码操作时才会用到,所以这里就不详细叙述了,仅把 System.BitConverter 类介绍给大家。

8. 转换成十六进制

  任何数据在计算机内部都是以二进制保存的,所以进制与数据的存储无关,只与输入输出有关。所以,对于进制转换,我们只关心字符串中的结果。
  在上面的第 4 条中提到了 ToString() 方法可以将数值转换成字符串,不过在字符串中,结果是以十进制显示的。现在我们带给它加一些参数,就可以将其转换成十六进制——使用 ToString(string) 方法。
  这里需要一个 string 类型的参数,这就是格式说明符。十六进制的格式说明符是 "x" 或者 "X",使用这两种格式说明符的区别主要在于 A-F 六个数字:"x" 代表 a-f 使用小写字母表示,而 "X" 而表示 A-F 使用大字字母表示。如下例:

private void TestHex() {
    int a = 188;
    this.textBox1.Text = "";
    this.textBox1.AppendText("a(10) = " + a.ToString() + "\n");
    this.textBox1.AppendText("a(16) = " + a.ToString("x") + "\n");
    this.textBox1.AppendText("a(16) = " + a.ToString("X") + "\n");
}

  运行结果如下:

a(10) = 188
a(16) = bc
a(16) = BC

  这时候,我们可能有另一种需求,即为了显示结果的整齐,我们需要控制十六进制表示的长度,如果长度不够,用前导的 0 填补。解决这个问题,我们只需要在格式说明符“x”或者“X”后写上表示长度的数字就行了。比如,要限制在 4 个字符的长度,可以写成“X4”。在上例中追加一句:

this.textBox1.AppendText("a(16) = " + a.ToString("X4") + "\n");

  其结果将输出 a(16) = 00BC。
  现在,我们还要说一说如何将一个表示十六进制数的字符串转换成整型。这一转换,同样需要借助于 Parse() 方法。这里,我需要 Parse(string, System.Globalization.NumberStyles) 方法。第一个参数是表示十六进制数的字符串,如“AB”、“20”(表示十进制的 32) 等。第二个参数 System.Globalization.NumberStyles 是一个枚举类型,用来表示十六进制的枚举值是 HexNumber。因此,如果我们要将“AB”转换成整型,就应该这样写:int b = int.Parse("AB", System.Globalization.NumberStyles.HexNumber),最后得到的 b 的值是 171。

9. 日期型数据和长整型数据之间的转换

  为什么要将日期型数据转换为长整型数据呢?原因很多,但就我个人来说,经常将它用于数据库的日期存储。由于各种数据库对日期型的定义和处理是不一样的,各种语言对日期型数据的定义的处理也各不相同,因为,我宁愿将日期型数据转换成长整型再保存到数据库中。虽然也可以使用字符串来保存,但使用字符串也会涉及到许多问题,如区域等问题,而且,它需要比保存长整型数据更多的空间。
  日期型数据,在 C# 中的参与运算的时候,应该也是转换为长整型数据来运算的。它的长整型值是自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。这个数在 C# 的 DateTime 中被称为 Ticks(刻度)。DateTime 类型有一个名为 Ticks 的长整型只读属性,就保存着这个值。如此,要从一个 DataTime 型数据得到 long 型值就非常简单了,只需要读出 DataTime 对象的 Ticks 值即可,如:

long longDate = DateTime.Now.Ticks;

  DateTime 的构造函数中也提供了相应的,从长整型数据构造 DateTime 型数据的函数:DateTime(long)。如:

DateTime theDate = new DateTime(longDate);

  但这样对于很多 VB6 程序员来说,是给他们出了一道难题,因为 VB6 中的日期型数据内部是以 Double 型表示的,将其转换为长整型后得到的仅仅是日期,而没有时间。如何协调这两种日期类型呢?
System.DateTime 提供了 double ToOADate() 和 static DateTime FromOADate(double) 两个函数来解决这个问题。前者将当前对象按原来的 double 值输出,后者则从一个 double 值获得一个 System.DateTime 对象。举例如下:

private void TestDateTimeLong() {
    double doubleDate = DateTime.Now.ToOADate();
    DateTime theDate = DateTime.FromOADate(doubleDate);
    this.textBox1.Text = "";
    this.textBox1.AppendText("Double value of now: " + doubleDate.ToString() + "\n");
    this.textBox1.AppendText("DateTime from double value: " + theDate.ToString() + "\n");
}

  运行结果:

Double value of now: 37494.661541713
DateTime from double value: 2002-8-26 15:52:37

10. 格式化日期型数据

  编程的过程中,通常需要将日期型数据按照一定的格式输出,当然,输出结果肯定是字符串。为此,我们需要使用 System.DateTime 类的 ToString() 方法,并为其指定格式字符串。
  MSDN 中,System.Globalization.DateTimeFormatInfo 类的概述里对模式字符串有非常详细的说明,因此,这里我只对常用的一些格式进行说明,首先请看下表:

d

月中的某一天

一位数的日期没有前导零

dd

月中的某一天

一位数的日期有一个前导零

ddd

周中某天的缩写名称

AbbreviatedDayNames 中定义

dddd

周中某天的完整名称

DayNames 中定义

M

月份数字

一位数的月份没有前导零

MM

月份数字

一位数的月份有一个前导零

MMM

月份的缩写名称

AbbreviatedMonthNames 中定义

MMMM

月份的完整名称

MonthNames 中定义

y

不包含纪元的年份

如果不包含纪元的年份小于 10,则显示不具有前导零的年份

yy

不包含纪元的年份

如果不包含纪元的年份小于 10,则显示具有前导零的年份

yyyy

包括纪元的四位数的年份

 

h

12 小时制的小时

一位数的小时数没有前导零

hh

12 小时制的小时

一位数的小时数有前导零

H

24 小时制的小时

一位数的小时数没有前导零

HH

24 小时制的小时

一位数的小时数有前导零

m

分钟

一位数的分钟数没有前导零

mm

分钟

一位数的分钟数有一个前导零

s

一位数的秒数没有前导零

ss

一位数的秒数有一个前导零

  为了便于大家的理解,不妨试试下面的程序:

private void TestDateTimeToString() {
    DateTime now = DateTime.Now;
    string format;
    this.textBox1.Text = "";
    format = "yyyy-MM-dd HH:mm:ss";
    this.textBox1.AppendText(format + ": " + now.ToString(format) + "\n");
    format = "yy年M日d日";
    this.textBox1.AppendText(format + ": " + now.ToString(format) + "\n");
}

  这段程序将输出结果:

yyyy-MM-dd HH:mm:ss: 2002-08-26 17:03:04
yy年M日d日: 02年8日26日

  这时候,又出现一个问题,如果要输出的文本信息中包含格式字符怎么办?如

format = "year: yyyy, month: MM, day: dd";
this.textBox1.AppendText(now.ToString(format) + "\n");

  将输出:

2ear: 2002, 4on下5: 08, 26a2: 26

  这并不是我想要的结果,怎么办呢?有办法——

format = "\"year\": yyyy, \'month\': MM, \'day\': dd";
this.textBox1.AppendText(now.ToString(format) + "\n");

  看,这次运行结果对了:

year: 2002, month: 08, day: 26

  可以看出,只需要使用单引号或者双引号将文本信息括起来就好。
  如果文本信息中包含双引号或者单引号又怎么办呢?这个问题,请读者们动动脑筋吧!

using System;

public class MyClass
{
public static void Main()
{
int i = 5;
Console.WriteLine(i);
System.Int32 val = i;
Console.WriteLine(val);
object myObj = i;
Console.WriteLine(myObj);
}
}

通过ildasm反编译,可以看到:

.method public hidebysig static void Main() cil managed
{
.entrypoint
// 代码大小 30 (0x1e)
.maxstack 1
.locals init ([0] int32 i,
[1] int32 val,
[2] object myObj)
IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: call void [mscorlib]System.Console::WriteLine(int32)
IL_0008: ldloc.0
IL_0009: stloc.1
IL_000a: ldloc.1
IL_000b: call void [mscorlib]System.Console::WriteLine(int32)
IL_0010: ldloc.0
IL_0011: box [mscorlib]System.Int32
IL_0016: stloc.2
IL_0017: ldloc.2
IL_0018: call void [mscorlib]System.Console::WriteLine(object)
IL_001d: ret
} // end of method MyClass::Main
posted @ 2007-04-29 15:06 阳光地带 阅读(173) 评论(0) 编辑