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<int>(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:謝謝支持!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM