您的位置:首页 > 数据库

反射一些基本知识,SQL

2008-06-19 14:51 459 查看
//获取类型的结构信息

  

   constructorinfo[] myconstructors=type.GetConstructors;

  

   //获取类型的字段信息

  

   fieldinfo[] myfields=type.GetFiedls()

  

   //获取方法信息

  

   MethodInfo myMethodInfo=type.GetMethods();

  

   //获取属性信息

  

   propertyInfo[] myproperties=type.GetProperties

  

   //获取事件信息

  

   EventInfo[] Myevents=type.GetEvents;

 

使用 Assembly 定义和加载程序集,加载在程序集清单中列出的模块,以及从此程序集中查找类型并创建该类型的实例。

使用 Module 了解如下的类似信息:包含模块的程序集以及模块中的类等。您还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。

使 用 ConstructorInfo 了解如下的类似信息:构造函数的名称、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetConstructors 或 GetConstructor 方法来调用特定的构造函数。

使用 MethodInfo 来了解如下的类似信息:方法的名称、返回类型、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法来调用特定的方法。

使用 FieldInfo 来了解如下的类似信息:字段的名称、访问修饰符(如 public 或 private)和实现详细信息(如 static)等;并获取或设置字段值。

使用 EventInfo 来了解如下的类似信息:事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等;并添加或移除事件处理程序。

使用 PropertyInfo 来了解如下的类似信息:属性的名称、数据类型、声明类型、反射类型和只读或可写状态等;并获取或设置属性值。

使用 ParameterInfo 来了解如下的类似信息:参数的名称、数据类型、参数是输入参数还是输出参数,以及参数在方法签名中的位置等。

.Net的应用程序由几个部分:‘程序集(Assembly)’、‘模块(Module)’、‘类型(class)’组成,而反射提供一种编程的方式,让程序员可以在程序运行期获得这几个组成部分的相关信息,

同程序集下

 System.Type type = System.Type.GetType(ConfigurationManager.AppSettings["LogType"].ToString());

            ILog log = (ILog)Activator.CreateInstance(type);

            log.Write(new Exception("异常测试"));

 如果在不同的程序集下,那主函数和配置会略有不同

string assemblyPath = Path.Combine(Environment.CurrentDirectory, "LogClassLibrary.dll");

            Assembly a = Assembly.LoadFrom(assemblyPath);

            Type type = a.GetType(ConfigurationManager.AppSettings["LogType"].ToString());

            LogClassLibrary.ILog log = (LogClassLibrary.ILog)type.InvokeMember(null,BindingFlags.CreateInstance,null,null,null);

             log.Write(new Exception("异常测试")); 

name      remark

1         11

1         12

1         13

2         21

2         22

2         23

3         31

3         32

3         33

变成

name remark1 remark2 remark3

1    11      12      13

2    21      22      23

3    31      32      33 

create table #T (name int,remark int)

insert into #T

select 1,11 union all

select 1,12 union all

select 1,13 union all

select 1,14 union all

select 2,21 union all

select 2,22 union all

select 2,23 union all

select 2,24 union all

select 3,31 union all

select 3,32 union all

select 3,33 union all

select 3,34

go

declare @sql varchar(8000)

set @sql='select name'

select @sql=@sql+',max(case when px='+ltrim(px)+' then remark end) as [remark'+ltrim(px)+']'

from (select distinct px from (select px=(select count(*) from #T where name=a.name and remark<=a.remark) from #T a) t) a

select @sql+' from (select *,px=(select count(*) from #T where name=a.name and remark<=a.remark) from #T a) t group by name'

exec(@sql+' from (select *,px=(select count(*) from #T where name=a.name and remark<=a.remark) from #T a) t group by name')

go

drop table #T

right('123456',3)     456  左边也同样

可以先生成视图,然后用工具生成实体类

多表查询

这两个都可以是否为空的

select * from 表 where 字段 is not null or 字段 <>''

exists 主要是用于WHERE的。

 

  IEnumerable<string> expr = from s in names

                             where s.Length == 5

                              orderby s

                              select s.ToUpper();

 foreach (string item in expr)

     Console.WriteLine(item);

 

 UNION 运算符

UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

说明:使用外连接

A、left outer join:

左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。

SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括

说明:日程安排提前五分钟提醒

SQL: select * from 日程安排 where datediff(minute,f开始时间,getdate())>5

select newid()

 

 

convert(varchar(12),Times,023) between @time1 and @time2

格式类型:   000   输出日期格式:   Sep   14   200  

  格式类型:   001   输出日期格式:   09/14/01    

  格式类型:   002   输出日期格式:   01.09.14    

  格式类型:   003   输出日期格式:   14/09/01    

  格式类型:   004   输出日期格式:   14.09.01    

  格式类型:   005   输出日期格式:   14-09-01    

  格式类型:   006   输出日期格式:   14   Sep   01    

  格式类型:   007   输出日期格式:   Sep   14,   01  

  格式类型:   008   输出日期格式:   17:06:08    

  格式类型:   009   输出日期格式:   Sep   14   200  

  格式类型:   010   输出日期格式:   09-14-01    

  格式类型:   011   输出日期格式:   01/09/14    

  格式类型:   012   输出日期格式:   010914    

  格式类型:   013   输出日期格式:   14   Sep   200  

  格式类型:   014   输出日期格式:   17:06:08:7  

  格式类型:   015   输出日期格式:   17:06:08:7  

  格式类型:   016   输出日期格式:   17:06:08:7  

  格式类型:   017   输出日期格式:   17:06:08:7  

  格式类型:   018   输出日期格式:   17:06:08:7  

  格式类型:   019   输出日期格式:   17:06:08:7  

  格式类型:   020   输出日期格式:   2001-09-14  

  格式类型:   021   输出日期格式:   2001-09-14  

  格式类型:   022   输出日期格式:   09/14/01    

  格式类型:   023   输出日期格式:   2001-09-14  

  格式类型:   024   输出日期格式:   17:06:08

 

在group by 再取到最大值  不是用到having的地方。 而是用到前面。

create table #ta(

id int identity(1,1) not null ,

volumeOfBusiness nvarchar(100) not null,

createTime datetime not null

)

go

insert into #ta values('A','2008-05-01 10:10:11');

insert into #ta values('B','2008-05-01 20:10:11');

insert into #ta values('C','2008-05-02 09:10:11');

insert into #ta values('D','2008-05-02 15:10:11');

insert into #ta values('E','2008-05-03 11:10:11');

Go

select #ta.* from #ta inner join 

(

select maxcreateTime = max(createTime), //这句很重要

datePartOfcreateTime = cast(year(createTime) as char(4)) + '-'

+ cast(month(createTime) as char(2)) + '-'

+ cast(day(createTime) as char(2))

FROM #ta group by 

cast(year(createTime) as char(4)) + '-'

+ cast(month(createTime) as char(2)) + '-'

+ cast(day(createTime) as char(2))

) temp

 on #ta.createTime = temp.maxcreateTime

drop table #ta

导数据的几种方式:

(1).使用SELECT INTO导出数据 

在使用SELECT INTO语句时,INTO后跟的表必须在数据库不存在,否则出错,数据结构也复制。

   SELECT * INTO table2 FROM table1   SELECT INTO只能将数据复制到一个空表中,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: