I always feel that I have been working for 6 years and experienced. It wasn't until recently that a. NET Advanced class was reported. I'm still a long way off. Take String.Join contrast.
My Code:
Public Static intInsertmodel<t> (T t)whereT:basemodel {Type type=typeof(T); stringColumnstrings =string. Join (",", type. GetProperties (). Select (p =string. Format ("[{0}]", P.name))); stringsql ="Insert into"+type. name+"("; foreach(varIteminchtype. GetProperties ()) {if(item. Name! ="Id") {SQL+=item. name+","; }} SQL= SQL. Substring (0Sql. Length-1); SQL+=")"; SQL+="VALUES ("; foreach(varIteminchtype. GetProperties ()) {if(item. Name! ="Id") {SQL+="'"+ Item. GetValue (t) +"',"; }} SQL= SQL. Substring (0Sql. Length-1); SQL+=")"; returndbhelpersql.executesql (SQL); }
Will find in the insert when I splicing more hard, and then the excellent work of our class compared a bit. Here's his code:
Public BOOLInsert<t> (T t)whereT:basemodel {//when inserting an entity, it is necessary to determine the time of insertion. You can also determine the length of the data by its characteristics.//The contract ID is self-growing. varType =typeof(T); varKeys =Newlist<string>(); varValues =Newlist<string>(); varPar =NewList<sqlparameter>(); foreach(varIteminchtype. GetProperties ()) {varValue =item. GetValue (t); if(item. Name.tolower () = ="ID")Continue; Keys. ADD (item. Name); Values. ADD ("@"+item. Name); Par. ADD (NewSqlParameter ("@"+item. Name, value)); } varc =string. Join (",", Keys. ToArray ()); varv =string. Join (",", values. ToArray ()); varsql =string. Format ($"INSERT into [{type. Name}] ({c})"+ $"VALUES ({v})"); returnexcutesql<BOOL> (sql, cmd = = { foreach(varParameterinchpar) cmd. Parameters.Add (parameter); varresult =cmd. ExecuteNonQuery (); returnResult >0; }); //return runcmd (SQL, par. ToArray ());}
He was found to have used string.join skillfully. I saved a lot of code compared to mine.
Later when the encourage each other, the code more on a layer!!!
C # learns from the basics string. Join (",", Keys.) ToArray ())