CYQ.Data V5 从入门到放弃ORM系列:教程 - MAction类使用

网友投稿 890 2022-11-27

CYQ.Data V5 从入门到放弃ORM系列:教程 - MAction类使用

CYQ.Data V5 从入门到放弃ORM系列:教程 - MAction类使用

背景:

随着V5框架使用者的快速增加,终于促使我开始对整个框架编写完整的Demo。

上周大概花了一星期的时间,每天写到夜里3点半,终完成了框架所有功能的Demo。

同时,按V5框架名称空间的顺序,对每个类的使用,补充相应的文章介绍,以漏补缺。

以下开始介绍:

MAction Demo的项目文件:

1:项目解决方案:

2:两个文件在Debug目录里:

演示的是SQLite数据库(默认System.Data.SQLite.DLL是64位版本,如果运行提示加载出错,自己解压32位的复盖即可)

demo.db的数据库结构为:(后续的Demo也以此两表为示例)

3:App.Config文件配置的是数据库链接

本类里面演示的是:单表、多表查询、多表操作,下面一个一个看:

单表操作:

1:界面:

2:代码

1 public partial class 单表操作 : Form 2 { 3 string tableName = "Users"; 4 public 单表操作() 5 { 6 AppConfig.DB.EditTimeFields = "EditTime";//该配置的字段,在更新时会自动被更新时间。 7 InitializeComponent(); 8 Pager.OnPageChanged += Pager_OnPageChanged; 9 } 10 11 void Pager_OnPageChanged(object sender, EventArgs e) 12 { 13 LoadData(); 14 } 15 16 17 18 private void 单表操作_Load(object sender, EventArgs e) 19 { 20 LoadData(); 21 22 } 23 private void LoadData() 24 { 25 MDataTable dt; 26 using (MAction action = new MAction(tableName)) 27 { 28 dt = action.Select(Pager.PageIndex, Pager.PageSize, "order by " + action.Data.PrimaryCell.ColumnName + " desc"); 29 OutDebugSql(action.DebugInfo); 30 } 31 if (dt != null && dt.Rows.Count > 0) 32 { 33 if (txtUserID.Text == "") 34 { 35 dt.Rows[0].SetToAll(this); 36 } 37 } 38 // dgView.DataSource = dt.ToDataTable(); 39 // 40 dt.Bind(dgView); 41 Pager.DrawControl(dt.RecordsAffected); 42 } 43 44 private void OutDebugSql(string msg) 45 { 46 if (string.IsNullOrEmpty(msg)) 47 { 48 msg = "Auto Cache...";//相关的配置,如:AppConfig.Cache.IsAutoCache = false; 49 } 50 rtxtSql.Text = msg; 51 } 52 53 private void btnFill_Click(object sender, EventArgs e) 54 { 55 using (MAction action = new MAction(tableName)) 56 { 57 if (action.Fill(txtUserID)) 58 { 59 action.UI.SetToAll(this); 60 OutDebugSql(action.DebugInfo); 61 } 62 } 63 } 64 65 private void btnInsert_Click(object sender, EventArgs e) 66 { 67 using (MAction action = new MAction(tableName)) 68 { 69 if (!action.Exists(txtName)) 70 { 71 action.AllowInsertID = chbInsertID.Checked; 72 action.UI.SetAutoParentControl(this);//Web开发的不需要这条 73 if (action.Insert(true, InsertOp.ID)) 74 { 75 action.UI.SetToAll(this); 76 LoadData(); 77 } 78 } 79 OutDebugSql(action.DebugInfo); 80 } 81 } 82 83 private void btnUpdate_Click(object sender, EventArgs e) 84 { 85 using (MAction action = new MAction(tableName)) 86 { 87 action.UI.SetAutoParentControl(this); 88 if (action.Update(true)) 89 { 90 LoadData(); 91 } 92 OutDebugSql(action.DebugInfo); 93 } 94 } 95 96 private void btnDelete_Click(object sender, EventArgs e) 97 { 98 using (MAction action = new MAction(tableName)) 99 {100 if (action.Delete(txtUserID))101 {102 LoadData();103 }104 OutDebugSql(action.DebugInfo);105 }106 }107 108 private void btnNoDelete_Click(object sender, EventArgs e)109 {110 AppConfig.DB.DeleteField = "IsDeleted";//演示用代码,一般配置在config对全局起使用。111 btnDelete_Click(sender, e);112 AppConfig.DB.DeleteField = "";113 }114 115 116 117 private void btn_Click(object sender, EventArgs e)118 {119 using (MAction action = new MAction(tableName))120 {121 action.Exists(txtUserID);122 action.Exists(txtName);//自动推导123 OutDebugSql(action.DebugInfo);124 }125 }126 private void btnOpenMutipleTable_Click(object sender, EventArgs e)127 {128 多表查询 m = new 多表查询();129 m.Show();130 }131 private void btnMutipleOperator_Click(object sender, EventArgs e)132 {133 多表操作 m = new 多表操作();134 m.Show();135 }136 137

3:补充讲解:

1:一开始的设想的Demo是:读数据库表(选择表)=》自动生成表单表=》然后实现上述的所有功能。

2:为了让新手看的容易明白,Demo走常规化,没写的那么自动化。

3:功能包含增删改查,检测存在,分页,排序等功能(事务在多表里演示)。

4:演示Demo中还有两个控件依赖(txtUserID,txtName),这两个也是可以传值的,所以整体是可无硬编码依存的。

多表查询:

1:界面:

2:代码:

1 public partial class 多表查询 : Form 2 { 3 public 多表查询() 4 { 5 AppDebug.Start(); 6 InitializeComponent(); 7 } 8 private void OutSql() 9 {10 rtxtSql.Text = AppDebug.Info;11 AppDebug.Stop();12 AppDebug.Start();13 }14 private void btnView_Click(object sender, EventArgs e)15 {16 MDataTable dt;17 using (MAction action = new MAction("V_Article"))18 {19 dt = action.Select();20 OutSql();21 }22 dt.Bind(dgvView);23 }24 25 private void btnSql_Click(object sender, EventArgs e)26 {27 string sql = "select a.*,u.Name from article a left join users u on a.UserID=u.UserID";28 MDataTable dt;29 using (MAction action = new MAction(sql))30 {31 dt = action.Select("order by userid desc");32 OutSql();33 }34 dt.Bind(dgvView);35 }36 37 private void btnJoin_Click(object sender, EventArgs e)38 {39 MDataTable dt;40 using (MAction action = new MAction("Article"))41 {42 dt = action.Select();43 44 }45 dt.JoinOnName = "UserID";46 dt = dt.Join("Users", "UserID", "Name");47 OutSql();48 dt.Bind(dgvView);49 50 }51

3:补充讲解:

有3种方法可以涉及多表

1:数据库里创建视图。

2:自定义SQL语句【原来是视图语句,这里内部自动补充成视图语句,增加小小的用户体验】(不能有排序,排序应放在where中)

3:MDataTable的Join方法(优点是:A:可以跨(不同种类的)数据库;B:可以增加自动缓存的利用率【都是单表操作,内存关联】)

多表操作:

1:界面:

2:代码:

1 public partial class 多表操作 : Form 2 { 3 public 多表操作() 4 { 5 InitializeComponent(); 6 } 7 private void OutSql(string msg) 8 { 9 rtxtSql.Text = msg; 10 } 11 private void LoadData(string where) 12 { 13 MDataTable dt; 14 using (MAction action = new MAction("V_Article")) 15 { 16 action.SetSelectColumns("UserID", "Name", "Title", "Content", "PubTime");//设置要显示的列 17 dt = action.Select(1, 100, where); 18 } 19 dt.Bind(dgvView); 20 } 21 private void btnTransation_Click(object sender, EventArgs e) 22 { 23 MDataTable dt = null; 24 string guid = Guid.NewGuid().ToString(); 25 using (MAction action = new MAction("Users")) 26 { 27 bool result = false; 28 action.SetTransLevel(IsolationLevel.ReadCommitted);//可设置的事务级别,一般可以不用设置 29 action.BeginTransation();//设置开启事务标识 30 action.Set("Name", guid.Substring(1, 5)); 31 action.Set("Password", "123456"); 32 int id = 0; 33 if (action.Insert())//第一个执行时,事务才被加载 34 { 35 id = action.Get(0); 36 action.ResetTable("Article"); 37 action.Set("UserID", id); 38 action.Set("Title", guid.Substring(3, 5)); 39 action.Set("Content", guid.Substring(5, 5)); 40 action.Set("PubTime", DateTime.Now); 41 result = action.Insert(InsertOp.None); 42 } 43 else 44 { 45 action.RollBack();//手工回滚 46 } 47 action.EndTransation();//提交事务 48 if (result) 49 { 50 LoadData("UserID=" + id); 51 } 52 OutSql(action.DebugInfo); 53 } 54 if (dt != null) 55 { 56 dt.Bind(dgvView); 57 } 58 } 59 60 private void 多表操作_Load(object sender, EventArgs e) 61 { 62 LoadData(null); 63 } 64 65 private void btnShowInfo_Click(object sender, EventArgs e) 66 { 67 StringBuilder sb = new StringBuilder(); 68 MDataTable dt = null; 69 using (MAction action = new MAction("Article")) 70 { 71 sb.Append("AllowInsertID:"); 72 sb.AppendLine(action.AllowInsertID.ToString()); 73 74 sb.Append("ConnectionString:"); 75 sb.AppendLine(action.ConnectionString); 76 77 sb.Append("DalType:"); 78 sb.AppendLine(action.DalType.ToString()); 79 80 sb.Append("DalVersion:"); 81 sb.AppendLine(action.DalVersion); 82 83 sb.Append("DebugInfo:"); 84 sb.AppendLine(action.DebugInfo); 85 86 sb.Append("RecordsAffected:(通常在执行一个命令后,返回受影响的行数)"); 87 sb.AppendLine(action.RecordsAffected.ToString()); 88 89 sb.Append("TableName:"); 90 sb.AppendLine(action.TableName); 91 92 sb.Append("TimeOut:"); 93 sb.AppendLine(action.TimeOut.ToString()); 94 95 sb.Append("UI对象:"); 96 sb.AppendLine(action.UI.ToString()); 97 98 dt = action.Data.Columns.ToTable(); 99 }100 dt.Bind(dgvView);101 rtxtSql.Text = sb.ToString();102 }103 104 private void btnPara_Click(object sender, EventArgs e)105 {106 MDataTable dt;107 using (MAction action = new MAction("Users"))108 {109 action.SetPara("Name", "0%", DbType.String);110 dt = action.Select("Name like @Name");111 }112 dt.Bind(dgvView);113 }114

3:补充讲解:

1:这里的演示比较单纯,并没有使用单表操作时批量操作(因为是自己造数据,没有界面或外界传值)。

2:一般SQL操作内部有异常,事务是会自动回滚的,只要判断true,false就可以了;

3:如果是自己的代码异常,或业务判断需要回滚,就RollBack()一下。

总结:

1:本次演示,并没有使用框架操作的ProjectTool去生成枚举(后续ORM名称空间的Demo是有生成实体的):常规项目时,生成枚举,可代替硬编码问题。

2:SVN里对应的Demo示例相对丰富,每个类都有Demo,当然也有个别我偷懒了(直接链接到了文章,哈)。

3:在整个写Demo的一周里,(1:处理偏冷的小问题,少量不常用的方法新增或减少),版本的升级也很频繁,目前稳定在V5.6.3.2版本。

4:Demo的SVN-:https://github.com/cyq1162/cyqdata

5:谢谢支持!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary
下一篇:解决sharding JDBC 不支持批量导入问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~