/// <summary> /// 新增方法 /// </summary> /// <param name="tablename">表名</param> /// <param name="rec">参数</param> /// <returns></returns> public ReResult Inserts(string tablename, List <Record> rec, bool transaction = false) { var re = new ReResult(); if (rec.IsBlank()) { return(re.setCode(400, "未添加有效数据")); } var list = rec.Select(x => new Curd(tablename, x, Operation.Insert)).ToList(); return(SaveBeans(list, transaction, re)); }
/// <summary> /// 删除方法 /// </summary> /// <param name="tablename">表名</param> /// <param name="cox">条件</param> /// <returns></returns> public ReResult Delete(string tablename) { var re = new ReResult(); if (tablename.IsBlank()) { return(re.setCode(400, "未添加有效数据")); } return(SaveBeans(new List <Curd>() { new Curd(tablename, new Record(), Operation.Clear) }, false, re)); }
/// <summary> /// 删除方法 /// </summary> /// <param name="tablename">表名</param> /// <param name="cox">条件</param> /// <returns></returns> public ReResult Delete(string tablename, Record cox, Record rec) { var re = new ReResult(); if (cox.IsBlank()) { return(re.setCode(400, "未添加有效数据")); } return(SaveBeans(new List <Curd>() { new Curd(tablename, cox, Operation.Delete, rec) }, false, re)); }
/// <summary> /// 新增方法 /// </summary> /// <param name="tablename">表名</param> /// <param name="rec">参数</param> /// <returns></returns> public ReResult Insert(string tablename, Record rec) { var re = new ReResult(); if (rec.IsBlank()) { return(re.setCode(400, "未添加有效数据")); } return(SaveBeans(new List <Curd>() { new Curd(tablename, rec, Operation.Insert) }, false, re)); }
/// <summary> /// 保存 /// </summary> /// <param name="rec">操作值</param> /// <param name="transaction">是否启用事务</param> /// <param name="rex">返回值</param> /// <returns></returns> public ReResult SaveBeans(List <Curd> rec, bool transaction = false, ReResult rex = null) { rex = rex ?? new ReResult(); var list = new List <Record>(); var con = Connection(); if (con.code != 200) { return(rex.setCode(con.code, con.msg)); } MySqlTransaction trans = null; if (transaction) { trans = conn.BeginTransaction(); } try { var sqls = new List <string>(); rec.ForEach(x => { if (x.op == Operation.Insert && !x.list.IsBlank()) { sqls.AddRange(x.getSqlList()); } else { var ss = x.getSql(); if (!ss.IsBlank()) { sqls.Add(ss); } } }); foreach (var sql in sqls) { if (sql.IsBlank()) { if (transaction && trans != null) { trans.Rollback(); } list.Add(new Record("success", "false").Put("message", "sql语句错误")); return(rex.setCode(400, "sql语句错误").setData(list)); } MySqlCommand cmd = new MySqlCommand(sql, conn); //执行ExecuteReader()返回一个MySqlDataReader对象 var i = cmd.ExecuteNonQuery(); if (i > 0) { list.Add(new Record("Success", "true").Put("Message", "").Put("Sql", sql)); continue; } if (transaction && trans != null) { trans.Rollback(); } list.Add(new Record("Success", "false").Put("Message", "保存操作失败").Put("Sql", sql)); return(rex.setCode(400, "操作失败").setData(list)); } if (transaction && trans != null) { trans.Commit(); } return(rex.setData(list)); } catch (Exception ex) { if (transaction && trans != null) { trans.Rollback(); } return(rex.setCode(500, ex.Message).setData(list)); } finally { Close(); } }