tijian_tieying/web/dccdc.DAL/newsinfoDal.cs
2025-02-20 12:14:39 +08:00

134 lines
4.4 KiB
C#

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 newsinfoDal
{
public List<newsinfoModel> GetAllTreeList()
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<newsinfoModel>("select * from news_info", new { @zt = 1 }).ToList();
}
}
public List<newsinfoModel> GetAllList()
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
return conn.Query<newsinfoModel>("select * from news_info where zt=1 ").ToList();
}
}
public List<newsinfoModel> GetListByKey(string key)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = "select * from news_info where zt=1";
if (!string.IsNullOrEmpty(key))
{
sql += " and title like @key";
}
return conn.Query<newsinfoModel>(sql, new { key = "%" + key + "%" }).ToList();
}
}
public newsinfoModel GetmodelByName(string name)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
List<newsinfoModel> model = conn.Query<newsinfoModel>("select * from news_info where title=@name", new { name = name }).ToList();
if (model.Any())
{
return model[0];
}
return null;
}
}
public List<newsinfoModel> GetAllList(string id)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string param = "";
if (id != "")
{
param = " and id=@id";
}
param += " order by id desc";
return conn.Query<newsinfoModel>("select * from news_info where 1=1" + param, new { @id = id }).ToList();
}
}
public bool Add(newsinfoModel model)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = @"INSERT INTO [dbo].[news_info]
([bt]
,[nr]
,[lx]
,[czy]
,[sj]
,[zt]
,[yd])
VALUES
(@bt
,@nr
,@lx
,@czy
,@sj
,@zt
,@yd)";
return (conn.Execute(sql, model) != 0 ? true : false);
}
}
public bool Update(newsinfoModel model)
{
using (IDbConnection conn = CommHelper.GetSqlConnection())
{
string sql = @"UPDATE [dbo].[news_info]
SET [bt] = @bt
,[nr] = @nr
,[lx] = @lx
,[czy] = @czy
,[sj] = @sj
,[zt] = @zt
,[yd] = @yd
WHERE id=@id";
return (conn.Execute(sql, model) != 0 ? true : false);
}
}
public object delete(string id)
{
string sql = "delete from news_info 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 };
}
}
}
}
}