CYQ. Data V5 from getting started to giving up the ORM series: tutorial, cyq. dataorm
MProc Introduction
MProc: a database operation class used to execute SQL or stored procedures. It is lightweight and high-performance similar to Dapper.
MProc: It rarely appears, because MAction itself can process 90%-100% of data operations (excluding stored procedures as the core operation)
MProc project Demo1: project diagram: there is only one console code, which indicates that this type of use is extremely simple.
2: Sample Code:
1 class Program 2 {3 4 static void Main (string [] args) 5 {6 // MAction has demonstrated the configuration file configuration link, and the code is used here. 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 // Execute SQL statement 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 (); // The use of transactions is the same as that of 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 (); // If no result is found, transaction 47 return; 48} 49 50 proc should be rolled back. resetProc ("select * from users; select * from Article"); // multiple statements run 51 List <MDataTable> dtList = proc. exeMDataTableList (); 52 OutMsg (dtList. count); 53 proc. endTransation (); 54} 55} 56 // <summary> 57 // execute the Stored Procedure 58 // </summary> 59 static void ExeProc () 60 {61 return; 62 // SQlite has no stored procedure and can only write sample code 63 using (MProc proc = new MProc ("Stored Procedure name") 64 {65 proc. set ("parameter 1", "value 1"); 66 proc. set ("parameter 2", "value 2"); 67 proc. setCustom ("ReturnValue", ParaType. returnValue); // if there is a returned value 68 proc. setCustom ("OutPutValue1", ParaType. outPut); // If the output value is 69 proc. setCustom ("OutPutValue2", ParaType. outPut); // If there are multiple output values, 70 proc. setCustom ("XXX", ParaType. cursor); // If Oracle has a Cursor 71 proc. setCustom ("XXX2", ParaType. CLOB); // Oracle's CLOB type 72 proc. setCustom ("XXX3", ParaType. NCLOB); // NCLOB type 73 MDataTable dt = proc. exeMDataTable (); // execute statement 74 int returnValue = proc. returnValue; // get the returned value 75 object outPutValue = proc. outPutValue; // if there is only one value 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: Code Description:
1: The MProc parameter determines whether the stored procedure or SQL statement is determined by space.
2: If your SQL statement is select % 20 * % 20from... escape space, it will be judged as a stored procedure.
3: If you really want to set this value, the third parameter isFixProc can be set to false or true to specify the SQL or stored procedure.
4: stored procedures: special parameters are set in SetCustom.
5: the return value and OutPut value are obtained only after execution. (Someone used to take the value before execution. I don't know how to explain it)
Summary:
1: Demo SVN: http://code.taobao.org/svn/cyqopen/trunk/CYQ.Data.GettingStarted/
2: Thank you for your support!