本系列所有文章索引:CYQ.Data 轻量数据层之路 框架开源系列 索引
本系列所有相关下载:CYQ.Data 轻量数据层之路 bug反馈、优化建议、最新框架下载
前言:
一直都是发布版本才写文章,这次为抢先体验版本[V2.5]做一下简单的功能介绍
以下进行功能更新说明[相比V2.0版本]:
1:修正DebugInfo属性在异常发生时无法取得操作语句的问题
2:MAction增加Bind方法可以轻松绑定DropDownList等控件
3:MDataTable增加ToList<T>泛型方法
2:MAction增加Bind方法可以轻松绑定DropDownList等控件
3:MDataTable增加ToList<T>泛型方法
4:修正MDataTable的ToJson方法
5:增加CYQ.Data.Orm.OrmBase抽象基类[为传统实体型ORM提供支持]
接着为功能进行示例操作,所有编写代码均放在Page_Load中:
一:调试信息输出
1:编写代码如下
MAction action = new MAction(TableNames.Users);
if (!action.Fill("产生错误"))
{
Response.Write(action.DebugInfo);
}
if (!action.Fill("产生错误"))
{
Response.Write(action.DebugInfo);
}
2:于是默认抛出异常
3:修改配置文件,启用日志记录,则不再抛异常
<appSettings><add key="IsWriteLog" value="true"/></appSettings>
4:这时候再运行,输出了DebugInfo
说明:
从上图可以看到所有数据库已执行的SQL语句,非常方便进行调试。
二:MAction的Bind功能演示
1:编写代码如下
new MAction(TableNames.Users).Bind(ddlUserName).Close();
说明:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
Bind方法有三个重载方法:
public MAction Bind(object control)
public MAction Bind(object control, string where)
public MAction Bind(object control, string where, object text, object value)
说明:前面两个方法按约定取[控件id(去掉三个字母前缀)做为text字段+ID为value字段]
public MAction Bind(object control)
public MAction Bind(object control, string where)
public MAction Bind(object control, string where, object text, object value)
说明:前面两个方法按约定取[控件id(去掉三个字母前缀)做为text字段+ID为value字段]
control参数支持继承ListControl的所有控件如:DrowDownList/CheckBoxList/RadioButtonList等
2:演示结果如图
3:对应的html
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<select name="ddlUserName" id="ddlUserName">
<option value="1">路过秋天</option>
<option value="2">狼Robot</option>
<option value="3">深蓝医生</option>
<option value="4">天才123</option>
<option value="5">天才123444</option>
<option value="9">goodboy</option>
<option value="17">goodboy</option>
</select>
<option value="1">路过秋天</option>
<option value="2">狼Robot</option>
<option value="3">深蓝医生</option>
<option value="4">天才123</option>
<option value="5">天才123444</option>
<option value="9">goodboy</option>
<option value="17">goodboy</option>
</select>
三:MDataTable的ToList<T>方法演示
1:增加实体Users
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UsersDemo
{
private int _ID;
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}
private string _Username;
public string UserName
{
get
{
return _Username;
}
set
{
_Username = value;
}
}
private string _Password;
public string Password
{
get
{
return _Password;
}
set
{
_Password = value;
}
}
private DateTime _CreateTime;
public DateTime CreateTime
{
get
{
return _CreateTime;
}
set
{
_CreateTime = value;
}
}
}
{
private int _ID;
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}
private string _Username;
public string UserName
{
get
{
return _Username;
}
set
{
_Username = value;
}
}
private string _Password;
public string Password
{
get
{
return _Password;
}
set
{
_Password = value;
}
}
private DateTime _CreateTime;
public DateTime CreateTime
{
get
{
return _CreateTime;
}
set
{
_CreateTime = value;
}
}
}
2:编写代码如下
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
MAction action = new MAction(TableNames.Users);
MDataTable table = action.Select();
action.Close();
List<UsersDemo> listDemo = table.ToList<UsersDemo>();//转成泛型实体列表
gvUsers.DataSource = listDemo;
gvUsers.DataBind();
MDataTable table = action.Select();
action.Close();
List<UsersDemo> listDemo = table.ToList<UsersDemo>();//转成泛型实体列表
gvUsers.DataSource = listDemo;
gvUsers.DataBind();
3:演示结果
四:MDataTable的ToJson方法演示
1:编写代码如下
using (MAction action = new MAction(TableNames.Users))
{
Response.Write(action.Select().ToJson());
}
{
Response.Write(action.Select().ToJson());
}
2:输出结果
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
{"count":"7","error":"","success":"true",
"data":[{"ID":"1","UserName":"路过秋天","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-8-21 14:01:07"},
{"ID":"2","UserName":"狼Robot","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-8-21 14:04:57"},
{"ID":"3","UserName":"深蓝医生","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-8-21 14:05:44"},
{"ID":"4","UserName":"天才123","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-9-4 14:57:28"},
{"ID":"5","UserName":"天才123444","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-9-4 15:00:20"},
{"ID":"9","UserName":"goodboy","Password":"abc","CreateTime":"2010-10-13 16:51:58"},{"ID":"17","UserName":"goodboy","Password":"333","CreateTime":"2010-10-13 17:09:34"}]}
"data":[{"ID":"1","UserName":"路过秋天","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-8-21 14:01:07"},
{"ID":"2","UserName":"狼Robot","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-8-21 14:04:57"},
{"ID":"3","UserName":"深蓝医生","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-8-21 14:05:44"},
{"ID":"4","UserName":"天才123","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-9-4 14:57:28"},
{"ID":"5","UserName":"天才123444","Password":"http://cyq1162.cnblogs.com","CreateTime":"2010-9-4 15:00:20"},
{"ID":"9","UserName":"goodboy","Password":"abc","CreateTime":"2010-10-13 16:51:58"},{"ID":"17","UserName":"goodboy","Password":"333","CreateTime":"2010-10-13 17:09:34"}]}
五:支持传统ORM访问方式示例演示
说明:本功能由 圣殿骑士 留言引发。
1:实体继承CYQ.Data.Orm.OrmBase
public class UsersDemo :CYQ.Data.Orm.OrmBase
{
public UsersDemo()
{
base.SetInit(this, "Users", "Conn");
}
//...省略下面积的实体属性
}
{
public UsersDemo()
{
base.SetInit(this, "Users", "Conn");
}
//...省略下面积的实体属性
}
说明:
1:需要继承基类:CYQ.Data.Orm.OrmBase
2:需要在构造函数初始化base.SetInit(this,"表名","数据库链接字符串/链接配置名称/为空时默认为Conn配置项");
2:需要在构造函数初始化base.SetInit(this,"表名","数据库链接字符串/链接配置名称/为空时默认为Conn配置项");
2:编写代码演示[添加/更新/删除/查询/列表查询与绑定]
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
protected void Page_Load(object sender, EventArgs e)
{
UsersDemo myUser = new UsersDemo();
myUser.UserName = "路过秋天-博客园";
myUser.Password = "http://cyq1162.cnblogs.com";
myUser.Insert();//插入一条数据
myUser.Password = "我更改了密码";
myUser.Update();
myUser.Delete(17);//删除id=17的数据
if (myUser.Fill(9))//单数据填充
{
Response.Write(myUser.UserName);
}
myUser.Select().Bind(gvUsers);//查询Users表所有数据并绑定到GrivdView控件
List<UsersDemo> entityList = myUser.Select().ToList<UsersDemo>();
gvUsers2.DataSource = entityList;//转泛型再绑定
gvUsers2.DataBind();
}
{
UsersDemo myUser = new UsersDemo();
myUser.UserName = "路过秋天-博客园";
myUser.Password = "http://cyq1162.cnblogs.com";
myUser.Insert();//插入一条数据
myUser.Password = "我更改了密码";
myUser.Update();
myUser.Delete(17);//删除id=17的数据
if (myUser.Fill(9))//单数据填充
{
Response.Write(myUser.UserName);
}
myUser.Select().Bind(gvUsers);//查询Users表所有数据并绑定到GrivdView控件
List<UsersDemo> entityList = myUser.Select().ToList<UsersDemo>();
gvUsers2.DataSource = entityList;//转泛型再绑定
gvUsers2.DataBind();
}
3:结果演示
a:原来表数据
b:操作后结果数据
结言:
谢谢大家支持与喜欢,吾将持续做好框架更新工作继续前进。
版权声明:本文原创发表于 博客园,作者为 路过秋天 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。 |
个人微信公众号![]() |
Donation(扫码支持作者):支付宝:![]() |
Donation(扫码支持作者):微信:![]() |
![]() |