using Dapper; using dccdc.Models; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace dccdc.DAL { public class kemu5Dal { public List GetAllTreeList() { using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query("select * from kemu5", new { @zt = 1 }).ToList(); } } public List GetAllList() { using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query("select * from kemu5 where zt=1 ").ToList(); } } public List GetListByKey(string key) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from kemu5 where zt=1"; if (!string.IsNullOrEmpty(key)) { sql += " and title like @key"; } return conn.Query(sql, new { key = "%" + key + "%" }).ToList(); } } public kemu5Model GetmodelByName(string name) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { List model = conn.Query("select * from kemu5 where title=@name", new { name = name }).ToList(); if (model.Any()) { return model[0]; } return null; } } public List GetAllList(string id) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string param = ""; if (id != "") { param = " and id=@id"; } return conn.Query("select * from kemu5 where 1=1" + param, new { @id = id }).ToList(); } } public bool Add(kemu5Model model) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = @"INSERT INTO [dccdc].[dbo].[kemu5] ([sort] ,[title] ,[content] ,[addtime] ,[zt]) VALUES (@sort ,@title ,@content ,@addtime ,@zt)"; return (conn.Execute(sql, model) != 0 ? true : false); } } public bool Update(kemu5Model model) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = @"UPDATE [dccdc].[dbo].[kemu5] SET [sort] = @sort ,[title] = @title ,[content] = @content ,[addtime] = @addtime ,[zt] = @zt WHERE id=@id"; return (conn.Execute(sql, model) != 0 ? true : false); } } public bool Update2(string title, string content) { kemu5Model m = new kemu5Model(); m.sort = ""; m.title = title; m.content = content; m.addtime = DateTime.Now; m.zt = 1; using (IDbConnection conn = CommHelper.GetSqlConnection()) { var model = conn.Query("select * from kemu5").FirstOrDefault(); string sql = ""; if (model == null) { sql = @"INSERT INTO [dccdc].[dbo].[kemu5] ([sort] ,[title] ,[content] ,[addtime] ,[zt]) VALUES (@sort ,@title ,@content ,@addtime ,@zt)"; } else { m.id = model.id; sql = @"UPDATE [dccdc].[dbo].[kemu5] SET [sort] = @sort ,[title] = @title ,[content] = @content ,[addtime] = @addtime ,[zt] = @zt WHERE id=@id"; } return (conn.Execute(sql, m) != 0 ? true : false); } } public object delete(string id) { string sql = "delete from kemu5 where id=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int result = conn.Execute(sql, new { id = id }); if (result > 0) { return new { State = 1, Message = "操作成功!" }; } else return new { State = 0, Message = "操作失败!" }; } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } } }