public long insert(IDataItem data) { if (data == null || data.count() == 0) { return(0); } List <Object> args = new List <Object>(); StringBuilder sb = new StringBuilder(); sb.Append(" INSERT INTO ").Append(_table).Append(" ("); data.forEach((key, value) => { if (value == null) { return; } sb.Append(_context.field(key)).Append(","); }); sb.DeleteCharAt(sb.Length - 1); sb.Append(") VALUES ("); data.forEach((key, value) => { if (value == null) { return; } if (value is String) { String val2 = (String)value; if (val2.Length > 0 && val2[0] == '$') //说明是SQL函数 { sb.Append(val2.Substring(1)).Append(","); } else { sb.Append("?,"); args.Add(value); } } else { sb.Append("?,"); args.Add(value); } }); sb.DeleteCharAt(sb.Length - 1); sb.Append("); SELECT @@IDENTITY;"); _builder.append(sb.ToString(), args.ToArray()); return(compile().insert()); }
public int update(IDataItem data) { if (data == null || data.count() == 0) { return(0); } List <Object> args = new List <Object>(); StringBuilder sb = new StringBuilder(); sb.Append("UPDATE ").Append(_table).Append(" SET "); data.forEach((key, value) => { if (value == null) { return; } if (value is String) { String val2 = (String)value; if (val2.Length > 0 && val2[0] == DataFlagConfig.sqlFunctionFlag[0]) { sb.Append(_context.field(key)).Append("=").Append(val2.Substring(1)).Append(","); } else { sb.Append(_context.field(key)).Append("=").Append(DataFlagConfig.paramReplaceFlag).Append(","); args.Add(value); } } else { sb.Append(_context.field(key)).Append("=").Append(DataFlagConfig.paramReplaceFlag).Append(","); args.Add(value); } }); sb.DeleteCharAt(sb.Length - 1); _builder.insert(sb.ToString(), args.ToArray()); return(compile().execute()); }
public long insert(IDataItem data) { if (data == null || data.count() == 0) { return(0); } List <Object> args = new List <Object>(); StringBuilder sb = new StringBuilder(); sb.Append(" INSERT INTO ").Append(_table).Append(" ("); data.forEach((key, value) => { if (value == null) { return; } sb.Append(_context.field(key)).Append(","); }); sb.DeleteCharAt(sb.Length - 1); sb.Append(") VALUES ("); data.forEach((key, value) => { if (value == null) { return; } if (value is String) { String val2 = (String)value; if (val2.Length > 0 && val2[0] == DataFlagConfig.sqlFunctionFlag[0]) //说明是SQL函数 { sb.Append(val2.Substring(1)).Append(","); } else { sb.Append(DataFlagConfig.paramReplaceFlag).Append(","); args.Add(value); } } else { sb.Append(DataFlagConfig.paramReplaceFlag).Append(","); args.Add(value); } }); sb.DeleteCharAt(sb.Length - 1); //检查当前数据库是否支持使用Select @@Identity if (_context.IsSupportSelectIdentityAfterInsert) { sb.Append("); SELECT @@IDENTITY;"); } else { sb.Append("); "); } _builder.append(sb.ToString(), args.ToArray()); return(compile().insert()); }