public static string GetUpdateSQL(BusinessObject BO) { string update; string where; string tableName; string sql; bool into = false; System.Text.StringBuilder Set = new System.Text.StringBuilder(); Set.Append(" SET "); where = " WHERE 1=1 "; tableName = BO.TableName; update = " UPDATE " + tableName + " "; FieldInfo[] fieldInfo = BO.GetType().GetFields(); for (int i = 0; i < fieldInfo.Length; i++) { into = false; System.Attribute[] attribute = System.Attribute.GetCustomAttributes(fieldInfo[i]); if (attribute.Length > 0) { for (int j = 0; j < attribute.Length; j++) { if (attribute[j].ToString() == "Wicresoft.BusinessObject.PrimaryKeyAttribute") { where = where + " AND " + InvokeMethod(BO, fieldInfo[i], "GetUpdateOrDeleteWhere"); PrimaryKeyAttribute keyattribute = (PrimaryKeyAttribute)attribute[j]; if (keyattribute.Policy == PrimaryKeyPolicy.Auto) { into = true; } } else if (attribute[j].ToString() == "Wicresoft.BusinessObject.CalculationFieldAttribute") // tony, 2008-07-25 { into = true; } } } if (into == false) { string fieldValue = InvokeMethod(BO, fieldInfo[i], "GetUpdateSet"); if (fieldValue != null) { Set.Append(fieldValue + ","); } } } sql = update + Set.ToString().Substring(0, Set.ToString().Length - 1) + where; Set.Remove(0, Set.Length); return(sql); }
public static string GetInsertSQL(BusinessObject BO, out bool haveIdentity, out FieldInfo mfieldinfo) { string tableName; string sql; System.Text.StringBuilder insert = new System.Text.StringBuilder(); System.Text.StringBuilder fields = new System.Text.StringBuilder(); System.Text.StringBuilder values = new System.Text.StringBuilder(); haveIdentity = false; mfieldinfo = null; tableName = BO.TableName; insert.Append(" Insert Into " + tableName); fields.Append(" ("); values.Append(" Values( "); FieldInfo[] fieldInfo = BO.GetType().GetFields(); for (int i = 0; i < fieldInfo.Length; i++) { string insertValue; bool into = false; System.Attribute[] attribute = Attribute.GetCustomAttributes(fieldInfo[i]); if (attribute.Length > 0) { into = false; for (int j = 0; j < attribute.Length; j++) { if (attribute[j].ToString() == "Wicresoft.BusinessObject.PrimaryKeyAttribute") { PrimaryKeyAttribute keyattribute = (PrimaryKeyAttribute)attribute[j]; if (keyattribute.Policy == PrimaryKeyPolicy.Auto) { haveIdentity = true; into = true; mfieldinfo = fieldInfo[i]; } } else if (attribute[j].ToString() == "Wicresoft.BusinessObject.CalculationFieldAttribute") // tony, 2008-07-25 { into = true; } } } if (into == false) { insertValue = InvokeMethod(BO, fieldInfo[i], "GetInsertValues"); if (insertValue != null) { fields.Append(InvokeMethod(BO, fieldInfo[i], "GetFieldName") + ","); values.Append(insertValue + ","); } } } sql = insert + fields.ToString().Substring(0, fields.ToString().Length - 1) + ") " + values.ToString().Substring(0, values.ToString().Length - 1) + " )"; insert.Remove(0, insert.Length); fields.Remove(0, fields.Length); values.Remove(0, values.Length); return(sql); }