MProc:是一個用於執行SQL或存儲過程的數據庫操作類,它輕量高性能地類似於Dapper。
MProc:它出現的場景很少,因為MAction自身就能處理掉90%-100%的數據操作(以存儲過程為核心操作的除外)

1 class Program
2 {
3
4 static void Main(string[] args)
5 {
6 //MAction已經演示了配置文件配置鏈接,這裡就用代碼了。
7 AppConfig.DB.DefaultConn = "Data Source={0}demo.db;failifmissing=false;";
8 ExeSql();
9 ExeProc();
10 Console.Read();
11 }
12 static void OutMsg(object msg)
13 {
14 Console.WriteLine(msg.ToString());
15 }
16 /// <summary>
17 /// 執行SQL語句
18 /// </summary>
19 static void ExeSql()
20 {
21 //AppConfig.DB.DefaultConn = "server=CYQ-PC\\SQL2008;database=Test;uid=sa;pwd=123456";
22 string sql = "select * from users";
23 using (MProc proc = new MProc(sql))
24 {
25 proc.BeginTransation();//事務的使用和MAction是一樣的
26
27 MDataTable dt = proc.ExeMDataTable();
28 OutMsg(dt.Rows.Count);
29 33
34 proc.ResetProc("select name from users where UserID=@UserID");
35 proc.Set("UserID", 1);
36 string name = proc.ExeScalar<string>();
37 OutMsg(name);
38
39 proc.ResetProc("update users set password=123 where name=@name");
40 proc.Set("name", name);
41 int result = proc.ExeNonQuery();
42 OutMsg(result);
43
44 if (result < 1)
45 {
46 proc.RollBack();//找不到結果,要回滾事務
47 return;
48 }
49
50 proc.ResetProc("select * from users;select * from Article");//多語句執行
51 List<MDataTable> dtList = proc.ExeMDataTableList();
52 OutMsg(dtList.Count);
53 proc.EndTransation();
54 }
55 }
56 /// <summary>
57 /// 執行存儲過程
58 /// </summary>
59 static void ExeProc()
60 {
61 return;
62 //SQlite 沒有存儲過程,只能寫示例代碼
63 using (MProc proc = new MProc("存儲過程名"))
64 {
65 proc.Set("參數1", "值1");
66 proc.Set("參數2", "值2");
67 proc.SetCustom("ReturnValue", ParaType.ReturnValue);//如果有返回值
68 proc.SetCustom("OutPutValue1", ParaType.OutPut);//如果有output值
69 proc.SetCustom("OutPutValue2", ParaType.OutPut);//如果有output值多個
70 proc.SetCustom("XXX", ParaType.Cursor);//如果是Oracle有游標
71 proc.SetCustom("XXX2", ParaType.CLOB);//Oracle的CLOB類型
72 proc.SetCustom("XXX3", ParaType.NCLOB);//Oracle的NCLOB類型
73 MDataTable dt = proc.ExeMDataTable();//執行語句
74 int returnValue = proc.ReturnValue;//拿返回值
75 object outPutValue = proc.OutPutValue;//如果只有一個值
76 Dictionary<string,string> dic=proc.OutPutValue as Dictionary<string,string>;
77 string out1 = dic["OutPutValue1"];
78 string out2 = dic["OutPutValue2"];
79 }
80 }
81 }
1:MProc的參數判斷是存儲過程還是SQL語句是按空格判斷的。
2:如果你的SQL語句是select%20*%20from...將空格轉義,會被判斷為存儲過程的。
3:如果你真要這麼整,第三個參數isFixProc可以設置為false或true來指定是SQL或存儲過程。
4:存儲過程時:特殊的參數在SetCustom裡設置。
5:返回值、OutPut值,都是在執行後才拿值的。(以前有人在執行前就拿值,弄的我不知道怎麼解釋)
1:Demo的SVN下載地址:http://code.taobao.org/svn/cyqopen/trunk/CYQ.Data.GettingStarted/
2:謝謝支持!