private string Trans2Sql(T t, dbOperType dot) { Type type = t.GetType(); string result = ""; PropertyInfo[] properties = type.GetProperties(); StringBuilder sb = new StringBuilder(); StringBuilder sIn = new StringBuilder(); foreach (PropertyInfo field in properties) { switch (dot) { case dbOperType.Select: sb.Append(field.Name).Append(","); break; case dbOperType.Insert: sb.Append(field.Name).Append(","); sIn.Append(DBUtils.GetSqlString(field.PropertyType, field.GetValue(t, null))).Append(","); break; case dbOperType.Update: sb.Append(field.Name).Append("=") .Append(DBUtils.GetSqlString(field.PropertyType, field.GetValue(t, null))) .Append(","); break; case dbOperType.Delete: break; } } if (sb.Length > 0) sb.Remove(sb.Length - 1, 1); if (sIn.Length > 0) sIn.Remove(sIn.Length - 1, 1); switch (dot) { case dbOperType.Select: result = "select " + sb.ToString() + " from " + type.Name; break; case dbOperType.Insert: result = "Insert into " + type.Name + "(" + sb.ToString() + ")"; result += " Values(" + sIn.ToString() + ")"; break; case dbOperType.Update: result = "Update " + type.Name + " set " + sb.ToString(); break; case dbOperType.Delete: result = "Delete from " + type.Name; break; } return result; }
2011-6-14 22:16 danny