NSun之快速开发NSunV3.0发布

       Lambda + NSunV1.5 = NSunV3.0,NSunV1.X及以前版本是在.Net Formwork 2.0平台上开发的,随着

.Net Formwork4.0的推出 ,.Net 3.5的普及率大幅提升,而NSunV3.0正是在.Net Formwork 3.5平台上加入了

其新的特性,此版本同样适用V1.5的操作方式以及实体类

       Lambda在LINQ以及扩展方法中相信很多人都对其非常熟悉。那么我就直接介绍NSunV3.0的操作使用。对于

NSunV1.5的修改有:新增Lambda查询,新增查询扩展方法。

       操作类从DBFactory中获得,这个类由生成器生成,可以修改数据库连接,包括多数据库连接以及数据库连接

加密。

   

实体类(Entity):

 

代码
[Serializable]
[Table(
"users")] //设置对应表名
public class UsersEntity : BaseEntity //继承BaseEntity
{
[PrimaryKeyAttribute(
true)] //设置主键,参数为是否自增长
public Int64 Id { get; set; }
public String Name { get; set; }
public String Pass { get; set; }
public DateTime? Logintime { get; set; }
}

操作类(DBFactory): 

// 设置连接对象
ConnectionData db = ConnectionData.Default;
// 设置操作对象
DBQuery<UsersEntity> dbuser = new DBQuery<UsersEntity>(db);

Create 

UsersEntity us = new UsersEntity()
{
Name
= "测试用户",
Pass
= "123",
Logintime
= DateTime.Now
};
// 如果实体主键为自增长时返回自增长编号,不是则返回1或0
dbuser.Save(us);

Update

  

代码
1、持久化实体进行修改
持久化对象通过查询或者插入得到。
us.Name
= "测试用户123";//修改名称
dbuser.Save(us); //保存
2、按条件与特定列修改
var update
= db.CreateUpdate();
update.Where(p1
=> p1.Id == 1);
update.AddColumn(p1
=> p1.Name, "测试");
update.Execute();
//也可以通过操作对象进行操作 us.Update(update);

Delete 

代码
1、主键删除
dbuser.Delete(
1);//按主键删除
dbuser.Delete(us);//通过实体删除,这里也是持久化对象
2、条件删除
var delete
= dbuser.CreateDelete();
delete.Where(p1
=> p1.Id == 2);
delete.Execute();
//也可以通过操作对象进行操作 us.Delete(delete);

Read

 1、Select

 

代码
var query = dbuser.CreateQuery();
query.Where(p1
=> p1.Id == 1);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
//查询重置
query.Where(p1 => p1.Id == 1 || p1.Id == 2);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Id > 7 && p1.Name.StartsWith("W"));
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Name.Like("%wui%"));
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Name.Length() == 2);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Pass.ToNumber() == 5);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Id.Between(1, 8));
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Id.In(1, 2, 3));
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Name.ToUpper() == "WUI");
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Name.ToLower() == "wui");
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p2
=> p2.Name.ToLower() == "wui");
query.Where
<UsInfo>(p2 => p2.Name.ToLower() == "name3");
Console.WriteLine(query.ToDbCommandText());

 2Order

 

代码
//order
query.Reset();
query.Where(p1
=> p1.Id != 1
);
query.OrderBy(p1
=>
p1.Id.Asc());
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Id != 1
);
query.OrderBy(p1
=> new
[] { p1.Id.Asc(), p1.Name.Desc(), p1.Pass.Asc() });
Console.WriteLine(query.ToDbCommandText());

3、Group

代码
//group
query.Reset(p1 => new { p1.Name, maxid = p1.Id.Max() });
query.Where(p1
=> p1.Id != 1
);
query.GroupBy(p1
=>
p1.Name);
Console.WriteLine(query.ToDbCommandText());

query.Reset(p1
=> new { p1.Name, maxid =
p1.Id.Max(),p1.Pass });
query.Where(p1
=> p1.Id != 1
);
query.GroupBy(p1
=> new
{ p1.Name, p1.Pass });
Console.WriteLine(query.ToDbCommandText());

//having

query.Reset(p1 => new { p1.Name, maxid = p1.Id.Max() });
query.Where(p1
=> p1.Id != 1
);
query.GroupBy(p1
=>
p1.Name);
query.Having(p1
=> p1.Id.Max() > 24
);
Console.WriteLine(query.ToDbCommandText());

4、Join

代码
//join test two
query.Reset();
query.Join
<UsInfo>((j1, i1) => j1.Id ==
i1.Id);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Join
<UsInfo>((j1, i1) => j1.Id ==
i1.Id);
query.Where
<UsInfo>((j1, i1) => j1.Name.Like("wui") && i1.Name == "name13"
);
Console.WriteLine(query.ToDbCommandText());

query.Reset
<UsInfo>((p1, p2) => new { p1.Name, p2name =
p2.Name });
query.Join
<UsInfo>((j1, i1) => j1.Id ==
i1.Id);
query.Where(j1
=> j1.Name == "wui"
);
query.Where
<UsInfo>(i1 => i1.Name == "name13"
);
query.OrderBy
<UsInfo>((p1, p2) => new
[] { p1.Id.Desc(), p2.Name.Desc() });
Console.WriteLine(query.ToDbCommandText());

query.Reset
<UsInfo>((p1, p2) => new { p1.Name, p2name =
p2.Name });
query.Join
<UsInfo>((j1, i1) => j1.Id ==
i1.Id);
query.Join
<StudentInfo, UsInfo>((j1, i1) => j1.Id ==
i1.Id);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.BindColumn(j1
=> new { j1.Name, j1.Logintime })//绑定显示列

.BindColumn<UsInfo>(i1 => new { i1.Name, i1.Id })
.BindColumn
<StudentInfo>(i1 => new
{ i1.Name, i1.Id });
query.Join
<UsInfo>((j1, i1) => j1.Id ==
i1.Id);
query.LeftJoin
<StudentInfo, UsInfo>((f1, e1) => f1.Id ==
e1.Id);
query.Where
<StudentInfo>(s1 => s1.Id == null
);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.BindColumn(j1
=> new { j1.Name, cout =
j1.Logintime.Count() });
query.Join
<UsInfo>((j1, i1) => j1.Id ==
i1.Id);
query.GroupBy(p1
=>
p1.Name);
Console.WriteLine(query.ToDbCommandText());

5、SelfFunction (自定义函数):

 

代码
query.Reset(p1 => new
{
idadd
= p1.Id.SelfMethodExtensions(
System.Data.DbType.AnsiString,
"dbo", "getidaddone"
)
});
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=>
p1.Id.SelfMethodExtensions(
System.Data.DbType.AnsiString,
"dbo", "getidaddone") == "12"
);
Console.WriteLine(query.ToDbCommandText());

query.Reset();
query.Where(p1
=> p1.Id.SelfMethodExtensions(System.Data.DbType.AnsiString, string.Empty, "getdate", false) == "20101025"
);
Console.WriteLine(query.ToDbCommandText());

6、ORM(扩展方法实现):

 

代码
public static class CustomersExtensionMethods
{
public static List<OrdersInfo> Orderss(this
CustomersInfo info)
{
return DBFactory.dbOrders.CreateQuery().Where<OrdersInfo>((j1, i1) => i1.CustomerID ==
j1.CustomerID).ToList();
}
}

//one to many

var ndb = DBFactory.dbOrders;
var nquery
=
ndb.CreateQuery();
nquery.Where(p1
=> p1.OrderID == 10248
);
var enorder
=
ndb.SelectToEntity(nquery);
Console.WriteLine(enorder.OrderID);
var encus
=
enorder.Customers();
Console.WriteLine(encus.CustomerID);

//many to one

var cdb = DBFactory.dbCustomers;
var cquery
=
cdb.CreateQuery();
cquery.Where(p1
=> p1.CustomerID == "VINET"
);
var cen
=
cdb.SelectToEntity(cquery);
Console.WriteLine(cen.Orderss().Count);

7、Linq原生查询

 

代码
var list = from S in DBFactory.dbuser select S ;
list
= list.Skip(0).Take(10
);
Console.WriteLine(list.Count());

var list2
= from p in DBFactory.dbUsers where p.Id > 2 && p.Name.Contains("a"
) select p;
Console.WriteLine(list2.Count());

 

注:实体生成器中可以选择V1.5实体,此版兼容V1.5操作。

 

最新更新:2011-06-10  20:10(核心包)

下载地址:

 

   NSun.CoreV3.0.rar(核心包)

       DbProvidersV3.0.rar(其他数据库扩展)

       EntityGenerateV3.0.rar(实体生成器)

       QuerySampleV3.0.rar(查询演示,内涵数据库表sql

       OtherDB_SqliteV3.0.rarSQLite数据库演示)

 

 

 

posted @ 2010-10-31 19:52  Dacey  Views(2374)  Comments(13Edit  收藏  举报