The legendary dynamic
Dynamic is a group of developers who do not follow the rules. It can be said to be an alien, but what's even more frightening is that it is completely unknown and it cannot be difficult to do anything, it seems to be opposite to lambda expressions ). This reminds me of the weird detective L in the diary of death, acting weird and super intelligent, so that the bizarre case had to be handed over to him. Dynamic can be seen as the embodiment of all types, but it is not limited to this, it is like the Terminator of the T-1000-type liquid metal in the sequel to futuristic warriors. Oh ~~~~ It seems a little far away.
Ado. net
ADO. net has always been well-founded, and is very focused on its own field. It is a good professional, it reminds me of the game theory and the field of differential geometry in the beautiful mind, which led to the Nobel Economics Award. Professor John Forbes Nash (Keke, professor Nash is an imaginary team ~~~ Well, I will talk about it later ).
Example of ADO. net
1. Execute SQL statements
Using(DbcommandCommand = connection. createcommand () {command. commandtext ="Select top 10 * from orders"; Command. commandtype =Commandtype. Text;Using(IdatareaderReader = command. executereader ()){While(Reader. Read ()){Console. Writeline ("Orderid: {0}, orderdate: {1 }", Reader. getint32 (reader. getordinal ("Orderid"), Reader. getdatetime (reader. getordinal ("Orderdate")));}}}
2. Call the Stored Procedure
Command. commandtext ="Custordersorders"; Command. commandtype =Commandtype. Storedprocedure; command. Parameters. Add (NewSqlparameter("Customerid","Alfki"));// Skip...
When ADO. Net encounters dynamic
In a certain month, ADO. Net unfortunately encountered dynamic, and the well-behaved life no longer exists. Dynamic says it can help ADO. net discards the burden of dataset, and implements different methods to pass query results without creating data entities. More powerful, it can be seamlessly connected to the stored procedure, that is, you can call a stored procedure just like calling a common method without having to write additionalCode. My God ~~~ After listening to dynamic, ADO. Net was surprised. Dynamic also said that to implement the project just mentioned, you only need to use your two major teams to help me with sqlconnection and sqlcommand.
Is dynamic really so amazing? ADO. net has doubts, but it once thought of seeing a movie named "A Gan Zheng Chuan". Although the argan in it was a mentally retarded person, he joined the army to practice Ping-Pong techniques and later competed with Chinese experts. With this in mind, ADO. Net believes that it cannot be because the other party is mentally retarded and does not trust the other party's words. This is a very rude and ungentle person who will do things, so it believes in dynamic.
Dynamic is truly disappointing.
Database Operations after dynamic Reconstruction
Using ( Dynamic Command = connection. createdynamiccommand ()){ // Execute the SQL query Ienumerable < Dynamic > Toptenorders = command ( "Select top 10 * from orders" ); Foreach ( Dynamic Order In Toptenorders ){ Console . Writeline ( "Orderid: {0}, orderdate: {1 }" , Order. orderid, order. orderdate );} // Execute the SQL statement with Parameters Ienumerable < Dynamic > Customerorders = command ( "Select * from orders where customerid = @ customerid" , Customerid: "Alfki" ); Foreach ( Dynamic Order In Customerorders ){Console . Writeline ( "Orderid: {0}, orderdate: {1 }" , Order. orderid, order. orderdate );} // Call the Stored Procedure Ienumerable < Dynamic > Orders = command. custordersorders (customerid: "Alfki" ); Foreach ( Dynamic Order In Orders ){ Console . Writeline ( "Orderid: {0}, orderdate: {1 }" , Order. orderid, order. orderdate );}}
You need to know that ADO. NET is not a. Net cainiao. It sees command ("Select top 10 * from orders"); The first thought is that the command taking the dynamic medicine may be the delegate type, and the command. custordersorders (customerid:"Alfki"); Had to reject the previous view. What is dynamic? We can think that dynamic is everything, or that dynamic is nothing!
ADO. net knows any. net's sophisticated code will show its prototype under reflector. By anatomy of the command, I immediately realized that I was entangled in a non-existent fantasy like Professor Nash of beautiful mind, reflector tells us that dynamic does not actually exist!
Uncle Lu Xun said well,There is no dynamic in the world, but Microsoft has encapsulated the delegation, and dynamic is also available.
Conclusion
Smart Do You Know How command is implemented? Think about it first, and then expand the following code to see if it is consistent with what you want.
Click here to expand the code
Note: The stored procedures section in this Article refer to micro Orm.
Public static classExtensions{Public staticDynamiccommandCreatedynamiccommand (ThisDbconnectionConnection ){Return newDynamiccommand(Connection );}}
/// <Summary> /// Dynamic command /// </Summary> Public class Dynamiccommand : Dynamicobject ,Idisposable { Public Dbconnection Connection { Get ; Set ;} Public Dynamiccommand ( Dbconnection Connection ){ This . Connection = connection ;} // Implement SQL statement Query Public override bool Tryinvoke ( Invokebinder Binder, Object [] ARGs,Out object Result ){ If (ARGs. Length = 0) Throw new Argumentexception ( "ARGs must has value" ); Result = execute (ARGs [0]. tostring (), Commandtype . Text, binder. callinfo. argumentnames, argS. Skip (1). toarray ()); Return true ;} // Implement the Stored Procedure Public override bool Tryinvokemember ( Invokememberbinder Binder,Object [] ARGs, Out object Result ){ If (Binder. callinfo. argumentnames. Count! = Binder. callinfo. argumentcount ){ Throw new Argumentexception ( "All parameters must be named" );} Result = execute (Binder. Name, Commandtype . Storedprocedure, binder. callinfo. argumentnames, argS ); Return true ;} /// <Summary> /// Execute SQL query /// </Summary> /// <Param name = "commandtext"> </param> /// <Param name = "commandtype"> </param> /// <Param name = "names"> </param> /// <Param name = "ARGs"> </param> /// <returns> </returns> Private object Execute ( String Commandtext, Commandtype Commandtype, Ienumerable < String > Names, Object [] ARGs ){ Bool Manageconnectionlifespan = ( This . Connection. State = Connectionstate . Closed ); If (Manageconnectionlifespan) This . Connection. open (); Try { Using ( VaR Cmd = This . Connection. createcommand () {cmd. commandtype = commandtype; cmd. commandtext = commandtext; For ( Int I = 0; I <args. length; I ++ ){ Dbparameter Param = cmd. createparameter (); Param. parametername = "@" + Names. elementat (I); Param. value = ARGs [I] = Null ? Dbnull . Value: ARGs [I]; cmd. Parameters. Add (PARAM );} Return Executelist (CMD );}} Finally { If (Manageconnectionlifespan ){This . Connection. Close ();}}} /// <Summary> /// Run the SQL command to return the query result list. /// </Summary> /// <Param name = "command"> </param> /// <returns> </returns> Private Static Ienumerable < Dynamic > Executelist ( Dbcommand Command ){ List < Dynamicentity > Resultlist =New List < Dynamicentity > (); Using ( Dbdatareader Reader = command. executereader ()){ While (Reader. Read ()){ Dynamicentity Entity = New Dynamicentity (); For ( Int I = 0; I <reader. fieldcount; I ++) {entity. setmember (reader. getname (I), reader. getvalue (I);} resultlist. add (entity );}} Return Resultlist ;}}
/// <Summary> /// Dynamic entity /// </Summary> Internal class Dynamicentity : Dynamicobject { /// <Summary> /// Attribute and value dictionary table /// </Summary> Private Dictionary < String , Object > Values = New Dictionary < String , Object > ( Stringcomparer . Ordinalignorecase ); Public override bool Trygetmember ( Getmemberbinder Binder, Out object Result ){ If (Values. containskey (Binder. Name) {result = values [binder. Name];} Else { Throw new System. Missingmemberexception ( "The property" + Binder. Name + "Does not exist" );} Return true ;} Public override bool Trysetmember ( Setmemberbinder Binder, Object Value) {setmember (Binder. Name, value ); Return true ;} Public override Ienumerable < String > Getdynamicmembernames (){ Return Values. Keys ;} Internal void Setmember ( String Propertyname, Object Value ){ If ( Object . Referenceequals (value, Dbnull . Value) {values [propertyname] = Null ;} Else {Values [propertyname] = value ;}}}