CYQ.Data 轻量数据层之路 使用篇二曲 MAction 数据查询(十三)

上一篇:CYQ.Data 轻量数据层之路 使用篇一曲 裸身走天涯(十二)

前言说明:

本篇继续上一篇内容,本节介绍所有相关查询的使用。

主要内容提要:
1:单行数据操作 Fill 操作 GetCount操作。
2:多行数据操作 Select 操作
3:列表绑定控件操作 配合分页控件
4:多表查询及绑定 视图及自定义SQL

单行数据操作

一:Fill 填充方法,单行查询

方法原形: public   bool  Fill( object   where )

示例1:直传ID

MAction action  =   new  MAction(TableNames.Users);
if  (action.Fill( 888 )) // 查询ID=888的单行数据
{
   action.SetTo(lblUserName);
   action.Close();
}

示例2:传where条件

扫描二维码关注公众号,回复: 6662514 查看本文章
MAction action  =   new  MAction(TableNames.Users);
if  (action.Fill( " id=888 or UserName='路过秋天'"))//查询ID=888或用户名为 " 路过秋天 " 的单行数据
{
   action.SetTo(lblUserName);
   action.Close();
}

示例3:where条件附带order by

MAction action  =   new  MAction(TableNames.Users);
if  (action.Fill( " id>888 order by id desc " )) // 查询ID>888的结果中取ID最大的的单行数据
{
   action.SetTo(lblUserName);
   action.Close();
}

二:GetCount 取统计总数

方法原形: public   int  GetCount( string   where )

示例1:

MAction action  =   new  MAction(TableNames.Users);
int  count = action.GetCount( " id>10 " );
action.Close();

多行数据操作

三:Select 多数据查询

方法原形:
1 public  MDataTable Select()
2 public  MDataTable Select( int  PageIndex,  int  PageSize,  string  Where,  out   int  RowCount)

示例1:

MAction action  =   new  MAction(TableNames.Users);
MDataTable tabme 
=  action.Select(); // 查询所有数据
action.Close();

示例2:

05233222_eh4C.gif
int  count; // 这个为返回的记录总数
MAction action  =   new  MAction(TableNames.Users);
MDataTable tabme 
=  action.Select( 1 , 10 , " id>10 order by username desc " , out  count); // 查询id>10的10条记录[第1页,每页10条数据,结果按usename排序]
action.Close();

附加说明:

Select 选择所有数据,方法内部原理为:
public  MDataTable Select()
{
   
int  count;
   
return  Select( 0 0 "" out  count);
}

列表绑定操作

四:绑定GridView/DataList/Repeater

示例1:查询所有直接绑定

MAction action  =   new  MAction(TableNames.Users);
MDataTable table 
=  action.Select();
action.Close();
gvUsers.DataSource 
=  table;
gvUsers.DataBind();

示例2:配合 分页控件 实战 Post篇 分页控件绑定 [下载地址:CYQ.Data 轻量数据层之路 bug反馈、优化建议、最新框架下载 ]

05233222_eh4C.gif
public   void  BindData()
{
        
int  count;
        MAction action 
=   new  MAction(TableNames.Users);
        MDataTable table 
=  action.Select(Pager1.PageIndex,Pager1.PageSize,  " id>10 " out  count);
        action.Close();
        gvUsers.DataSource 
=  table;
        gvUsers.DataBind();
        Pager1.Count 
=  count; // 设置记录总数
        Pager1.BindName  =   " BindData " ; // 绑定方法名称
}

示例3:配合其它Get方式分页控件绑定

05233222_eh4C.gif
public   void  BindData()
{
        
int  count;
        MAction action 
=   new  MAction(TableNames.Users);
        MDataTable table 
=  action.Select(Pager1.PageIndex,Pager1.PageSize,  " id>10 " out  count);
        action.Close();
        gvUsers.DataSource 
=  table;
        gvUsers.DataBind();
        Pager1.Count 
=  count;
}

说明:

如果你使用的分页控件比上面的使用情况复杂,你可以考虑优化或弃用原有的分页控件了。

多表查询及绑定

五:视图方式

示例1:和表操作一样,唯一区别就是表名换成视图名称

05233222_eh4C.gif
public   void  BindData()
{
        
int  count;
        MAction action 
=   new  MAction(ViewNames.V_Users);
        MDataTable table 
=  action.Select(Pager1.PageIndex,Pager1.PageSize,  " id>10 " out  count);
        action.Close();
        gvUsers.DataSource 
=  table;
        gvUsers.DataBind();
        Pager1.Count 
=  count;
        Pager1.BindName 
=   " BindData " ;
}

六:自定义构造多表SQL语句

示例1:

05233222_eh4C.gif
public   void  BindData()
{
      
string  customTable  =   " (select u.*,m.Body from Users u left join Message m on u.ID=m.UserID) v " ;
      
int  count;
      MAction action 
=   new  MAction(customTable);
      MDataTable table 
=  action.Select(Pager1.PageIndex,Pager1.PageSize,  " id>10 " out  count);
      action.Close();
      gvUsers.DataSource 
=  table;
      gvUsers.DataBind();
      Pager1.Count 
=  count;
      Pager1.BindName 
=   " BindData " ;
}

说明:

在具体使用过程中,为了方便管理,直接出现在自定义SQL语句就不这样直接写在界面中了,可以新项建一个项目统一管理自定义的SQL。

结言:

看完本篇示例,对于查询这一块应该明白了。配合起分页控件起来,实现还是很简单的。
其它用法请关注下一篇:名称未定。

转载于:https://my.oschina.net/secyaher/blog/274074

猜你喜欢

转载自blog.csdn.net/weixin_34326429/article/details/91966981