public DbStructContainer Insert <T>(T obj) where T : class, new() { string query = string.Empty; DbStructContainer container = TryGetTableFields(obj); if (container == null) { return(container); } container.OperateType = 2; DbFieldBase[] oFields = container.SQLFields; string[] fields = new string[oFields.Length]; string[] values = new string[oFields.Length]; for (int i = 0; i < oFields.Length; i++) { DbFieldBase oBaseField = oFields[i]; fields[i] = oBaseField.FieldName; string sValue = oBaseField.Value == null ? string.Empty : oBaseField.Value.ToString(); sValue = sValue.Replace("\"", "__SY__"); // 为了防止数据内容因为引号导致无法输入, 故此进行引号转换 values[i] = "\"" + sValue + "\""; } string sFields = string.Join(",", fields); string sValues = string.Join(",", values); container.SQLQuery = string.Format("insert into {0}({1}) values ({2})", container.SQLTable, sFields, sValues); return(container); }
public DbStructContainer Modify <T>(T obj) where T : class, new() { string query = string.Empty; DbStructContainer container = TryGetTableFields(obj); if (container == null) { return(container); } container.OperateType = 3; DbFieldBase[] oFields = container.SQLFields; string[] keyvalues = new string[oFields.Length]; for (int i = 0; i < oFields.Length; i++) { DbFieldBase oBaseField = oFields[i]; string sValue = oBaseField.Value == null ? string.Empty : oBaseField.Value.ToString(); sValue = sValue.Replace("\"", "__SY__"); // 为了防止数据内容因为引号导致无法输入, 故此进行引号转换 keyvalues[i] = string.Format("{0}=\"{1}\"", oBaseField.FieldName, sValue); } string sKeyValues = string.Join(",", keyvalues); container.SQLQuery = string.Format("update {0} set {1} where Guid=\"{2}\"", container.SQLTable, sKeyValues, container.SQLFields[container.SQLFields.Length - 1].Value); // 基类的Guid在最后一位,故而这里使用.Length - 1 return(container); }
public DbStructContainer TryGetTableFields(object obj) { string sSQLTable = string.Empty; DbStructContainer container = null; List <DbFieldBase> oFieldList = new List <DbFieldBase>(); Type type = obj.GetType(); object[] oSaveDBAttributes = type.GetCustomAttributes(typeof(DbAttribute), false); if (oSaveDBAttributes.Length > 0) { sSQLTable = (oSaveDBAttributes[0] as DbAttribute).TableName; FieldInfo[] fieldInfos = type.GetFields(); for (int i = 0; i < fieldInfos.Length; i++) { FieldInfo fieldInfo = fieldInfos[i]; object[] oFieldUnSaveDBAttributes = fieldInfo.GetCustomAttributes(typeof(DbUnsaveAttribute), false); if (oFieldUnSaveDBAttributes.Length == 0) { DbFieldBase oField = mConvert.Type2Object(fieldInfo.FieldType); oField.FieldName = fieldInfo.Name; oField.Value = fieldInfo.GetValue(obj); oFieldList.Add(oField); } } container = new DbStructContainer(); container.SQLTable = sSQLTable; container.SQLFields = oFieldList.ToArray(); } return(container); }
public void CopyFrom(DbStructContainer container) { SQLTable = container.SQLTable; SQLQuery = container.SQLQuery; SQLFields = new DbFieldBase[container.SQLFields.Length]; for (int i = 0; i < SQLFields.Length; i++) { DbFieldBase oBaseField = new DbFieldBase(); oBaseField.CopyFrom(container.SQLFields[i]); SQLFields[i] = oBaseField; } }
public DbStructContainer Delete <T>(T obj) where T : class, new() { string query = string.Empty; DbStructContainer container = TryGetTableFields(obj); if (container == null) { return(container); } container.OperateType = 1; container.SQLQuery = string.Format("delete from {0} where Guid=\"{1}\"", container.SQLTable, container.SQLFields[container.SQLFields.Length - 1].Value); // 基类的Guid在最后一位,故而这里使用.Length - 1 return(container); }
public DbStructContainer SearchAll <T>() where T : class, new() { string query = string.Empty; DbStructContainer container = TryGetTableFields(new T()); if (container == null) { return(container); } container.OperateType = 5; DbFieldBase[] oFields = container.SQLFields; string[] fields = new string[oFields.Length]; for (int i = 0; i < oFields.Length; i++) { fields[i] = oFields[i].FieldName; } string sField = string.Join(",", fields); container.SQLQuery = string.Format("select {0} from {1}", sField, container.SQLTable); return(container); }
public DbStructContainer Search <T>(T obj) where T : class, new() { string query = string.Empty; DbStructContainer container = TryGetTableFields(obj); if (container == null) { return(container); } container.OperateType = 4; DbFieldBase[] oFields = container.SQLFields; string[] fields = new string[oFields.Length]; for (int i = 0; i < oFields.Length; i++) { fields[i] = oFields[i].FieldName; } string sField = string.Join(",", fields); container.SQLQuery = string.Format("select {0} from {1} where Guid=\"{2}\"", sField, container.SQLTable, container.SQLFields[container.SQLFields.Length - 1].Value); // 基类的Guid在最后一位,故而这里使用.Length - 1 return(container); }