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

2年前 (2022) 程序员胖胖胖虎阿
274 0 0

MProc介绍

MProc:是一个用于执行SQL或存储过程的数据库操作类,它轻量高性能地类似于Dapper。

MProc:它出现的场景很少,因为MAction自身就能处理掉90%-100%的数据操作(以存储过程为核心操作的除外)

MProc项目Demo

1:项目图:只有一个控制台代码,说明此类的使用极度简单。

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

2:示例代码:

 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     }

3:代码说明:

 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:谢谢支持!

相关文章

暂无评论

暂无评论...