开源数据访问组件Smark.Data 1.7新增功能

[来源] 达内    [编辑] 达内   [时间]2012-10-16

借助于扩展方法的功能,这一版本的改进可以让数据库操作变得更简单方便,在数据库操作相关会给你一种全新的体验

借助于扩展方法的功能,这一版本的改进可以让数据库操作变得更简单方便,在数据库操作相关会给你一种全新的体验.主要改进有以下几点:

  • 条件表达式可以直接和string进行组合,从而更灵活地创建复杂的查询条件
  • 扩展Load_方法,可以直接在值对象的基础上获取相关ID数据对象
  • 扩展一系的查询方法,可直接在string的sql上进行数据操作,包括查询获取查询对象数据.在操作过程完全不需要借助于其他对象来处理.
  • 扩展可以直接在存储过程对象进行执行操作
以上的一系列扩展都是直接能操作数据,而不再需引用相关条件对象或数据库上下文对象,让数据操作变得更简单灵活.
< h2 style="margin-top: 10px; color: rgb(0, 0, 0); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">对象获取和保存 < blockquote style="background-image: none; border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); margin-left: 25px; padding: 5px 10px; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">
< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 829px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
2
3
var emp = 3.Load_<Employee>();
emp.Notes += "_test" ;
emp.Save_();

以上就是简单地根据ID值获取对象,修改并保存.由于扩展方法容易导致和其他方法冲突,所以在方法添加了_以便区分.

< h2 style="margin-top: 10px; color: rgb(0, 0, 0); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">SQL执行 < blockquote style="background-image: none; border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); margin-left: 25px; padding: 5px 10px; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">

在原来的版本时处理一些SQL执行,必须借助于一些对象.就是必须创建一个SQL执行对象来处理SQL.但在1.7版中就不再需要这样做,直接在一个SQL的String基础上就能执行操作.

< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 812px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
"delete * from employees" .Execute();

以上是简单地执行一个delete的sql语,当然也可以执行一个查询并返回一个对象列表

< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 829px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
2
3
4
foreach  (var item in  "select * from employees" .List<Employee>())
{
     Console.WriteLine(item.FirstName);
}

查询需要设置相关参数

< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 829px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
2
3
4
foreach  (var order in  "select * from orders where employeeid=@p1" .Parameter( "p1" , 3).List<Order>())
{
     Console.WriteLine( "orderid:{0}\t orderdate:{1}" , order.OrderID, order.OrderDate);
}
< h2 style="margin-top: 10px; color: rgb(0, 0, 0); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">存储过程执行 < blockquote style="background-image: none; border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); margin-left: 25px; padding: 5px 10px; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">
< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 829px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
2
3
CustOrderHist coh = new   CustOrderHist();
coh.CustomerID = "ALFKI" ;
coh.ExecProc_();

只需要创建一个存储过程描述对象,设置好相关参数值就可以直接执行处理,当然也可以执行并返回一个查询结果.

< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 829px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
2
3
4
5
6
CustOrderHist coh = new   CustOrderHist();
coh.CustomerID = "ALFKI" ;
foreach  (var data in  coh.ExecProcToObjects_<CustOrderHistData>())
{
     Console.WriteLine( "{0}\t{1}" , data.ProductName, data.Total);
}
< h2 style="margin-top: 10px; color: rgb(0, 0, 0); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">对象条件和string描述的条件整合 < blockquote style="background-image: none; border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); margin-left: 25px; padding: 5px 10px; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">

在原有的条件表达式中处理复杂的SQL查询变得比较麻烦,实际情况下一些复杂的条件的确没有直接SQL来得直观,因此新版条件表达式可以直接和string描述的条件整合并产生新的表达式对象进行数据操作.

< div style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: none !important; bottom: auto !important; float: none !important; height: 11px !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: absolute !important; right: 1px !important; text-align: left !important; top: 1px !important; vertical-align: baseline !i mportant; width: 11px !important; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 10px !important; min-height: inherit !important; z-index: 10 !important; color: rgb(255, 255, 255) !important; background-position: initial initial !important; background-repeat: initial initial !important; " class="toolbar"> ?
< table cellspacing="0" cellpadding="0" border="0" style="border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 1px solid rgb(192, 192, 192); bot tom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 2em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-align: left !im portant; top: auto !important; vertical-align: baseline !important; width: 829px; box-sizing: content-box !important; font-family: 'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important; font-weight: normal !important; font-style: normal !important; font-size: 12px !important; min-height: inherit !important; border-collapse: collapse; background-position: initial initial !important; background-repeat: initial initial !important; ">
1
2
3
4
5
6
7
Expression exp = new   Expression();
exp &=  Order.orderDate >= "1996-7-1" ;
exp &= " employeeid in (select employeeid from employees where region=@p1)" .Add( "p1" , "WA" );
foreach  (var order in  exp.List<Order>())
{
     Console.WriteLine( "orderid:{0}\t orderdate:{1}" , order.OrderID, order.OrderDate);
}

资源下载