DIY 的 LinQ
先要感谢henry提供的很多宝贵意见和经验,让我少走了很多弯路
这几天用泛型运算符重载的办法封装了SQL语句,让C#的代码直接用于数据库查询,有点像C# 3.0里的LinQ,呵呵。
还在不断完善中,先放上来Show一下 。
像了解泛型运算符重载请看:http://www.cnblogs.com/BG5SBK/archive/2006/07/13/GenericOperatorOverride.html
增:
删:
改:
查:
符合查询:
排序查询:
As支持:
基本就这些了,呵呵,随便取名叫DIY的 LinQ,大家不要扔石头啊
这几天用泛型运算符重载的办法封装了SQL语句,让C#的代码直接用于数据库查询,有点像C# 3.0里的LinQ,呵呵。
还在不断完善中,先放上来Show一下 。
像了解泛型运算符重载请看:http://www.cnblogs.com/BG5SBK/archive/2006/07/13/GenericOperatorOverride.html
增:
//Insert Into [Member] ([Code],[JoinTime],[Name],[Password]) values(@P1,@P2,@P3,@P4)
Insert _insert = new Insert(Member.Columns._Table,
Member.Columns.Code.Set(Guid.NewGuid().ToString()),
Member.Columns.JoinTime.Set(DateTime.Now),
Member.Columns.Name.Set("xxx"),
Member.Columns.Password.Set(CommonCode.HashEncrypt("123", 0)));
SqlQuery.DefaultInstance.ExecuteInsert(_insert);
Insert _insert = new Insert(Member.Columns._Table,
Member.Columns.Code.Set(Guid.NewGuid().ToString()),
Member.Columns.JoinTime.Set(DateTime.Now),
Member.Columns.Name.Set("xxx"),
Member.Columns.Password.Set(CommonCode.HashEncrypt("123", 0)));
SqlQuery.DefaultInstance.ExecuteInsert(_insert);
删:
//Delete From [Member] Where [Member].[Name]=@P1 Or [Member].[JoinTime]=@P2
Delete _delete = new Delete(Member.Columns._Table, Member.Columns.Name == "yyy" | Member.Columns.JoinTime == DateTime.Now);
SqlQuery.DefaultInstance.ExecuteDelete(_delete);
Delete _delete = new Delete(Member.Columns._Table, Member.Columns.Name == "yyy" | Member.Columns.JoinTime == DateTime.Now);
SqlQuery.DefaultInstance.ExecuteDelete(_delete);
改:
//Update Member Set [Name]=@P2 Where [Member].[Name]=@P1
Update _update = new Update(Member.Columns._Table, Member.Columns.Name == "xxx", Member.Columns.Name.Set("yyy"));
SqlQuery.DefaultInstance.ExecuteUpdate(_update);
Update _update = new Update(Member.Columns._Table, Member.Columns.Name == "xxx", Member.Columns.Name.Set("yyy"));
SqlQuery.DefaultInstance.ExecuteUpdate(_update);
查:
//Select [Member].* From [Member] Where [Member].[Name]=@P1 And [Member].[Password]=@P2
Select _select = new Select(Member.Columns._Any).From(Member.Columns._Table).Where(
Member.Columns.Name == Member.Columns.Password & Member.Columns.Password == password);
List<Member> _members = SqlQuery.DefaultInstance.ExecuteList<Member>(_select);
Select _select = new Select(Member.Columns._Any).From(Member.Columns._Table).Where(
Member.Columns.Name == Member.Columns.Password & Member.Columns.Password == password);
List<Member> _members = SqlQuery.DefaultInstance.ExecuteList<Member>(_select);
符合查询:
//Select [Member].*, [ToDoList].* From [Member],[ToDoList] Where [Member].[Code]=[ToDoList].[UserCode]
Select _select = new Select(Member.Columns._Any, ToDoList.Columns._Any);
_select.From(Member.Columns._Table, ToDoList.Columns._Table);
_select.Where(Member.Columns.Code == ToDoList.Columns.UserCode);
System.Data.DataSet _result = SqlQuery.DefaultInstance.ExecuteDataSet(_select);
Select _select = new Select(Member.Columns._Any, ToDoList.Columns._Any);
_select.From(Member.Columns._Table, ToDoList.Columns._Table);
_select.Where(Member.Columns.Code == ToDoList.Columns.UserCode);
System.Data.DataSet _result = SqlQuery.DefaultInstance.ExecuteDataSet(_select);
排序查询:
//Select [Member].* From [Member] Order By [Member].[JoinTime] desc
Select _select = new Select(Member.Columns._Any).From(Member.Columns._Table).OrderBy(Member.Columns.JoinTime.Desc);
List<Member> _members = SqlQuery.DefaultInstance.ExecuteList<Member>(_select);
Select _select = new Select(Member.Columns._Any).From(Member.Columns._Table).OrderBy(Member.Columns.JoinTime.Desc);
List<Member> _members = SqlQuery.DefaultInstance.ExecuteList<Member>(_select);
As支持:
//Select [Table1].[Name] as [UserName] From [Member] as [Table1]
Select _select = new Select(Member.Columns.Name.At("Table1").As("UserName")).From(Member.Columns._Table.As("Table1"));
List<Member> _members = SqlQuery.DefaultInstance.ExecuteList<Member>(_select);
Select _select = new Select(Member.Columns.Name.At("Table1").As("UserName")).From(Member.Columns._Table.As("Table1"));
List<Member> _members = SqlQuery.DefaultInstance.ExecuteList<Member>(_select);
基本就这些了,呵呵,随便取名叫DIY的 LinQ,大家不要扔石头啊