Static voidMain (string[] args) { Book Book=NewBook ();//entity classBooktest B1 =Newbooktest (); Book.bookid="1"; Book.bookname="Computer Principles"; Book.bookprice=32.04M; stringsql =createinsertsql (book); } Public Static stringcreateinsertsql (Book book) {Type type=Book . GetType (); propertyinfo[] Props=type. GetProperties (); StringBuilder SB=NewStringBuilder (); Sb. Append ("Insert into"+ type. name+"("); foreach(PropertyInfo propinchprops) { stringName =Prop. Propertytype.fullname; stringValue = Prop. GetValue (book,NULL) as string; Object[] array = Prop. GetCustomAttributes (typeof(Keyattribute),true);//Gets the attribute that is required in the SQL statement, such as the primary key if(Array. Length >0) { Continue; } sb. Append (Prop. Name+","); } sb. Remove (sb.) Length-1,1); Sb. Append (") VALUES ("); foreach(PropertyInfo propinchprops) { Object[] array = Prop. GetCustomAttributes (typeof(Keyattribute),true); if(Array. Length >0) { Continue; } sb. Append ("@"+ Prop. Name +","); } sb. Remove (sb.) Length-1,1); Sb. Append (")"); returnsb. ToString (); }View Code
C # reflection generates simple SQL statements