Persister使用说明

 

1】加载单个对象
Persister<NewsDetail>.Load(sysNo);

2】查询列表
DateTime dtNow = DateTime.Now.Date;
WhereClauseBuilder<NewsMaster> where = new WhereClauseBuilder<NewsMaster>();
where.And(f => f.Status == 3);
if (newsType.HasValue)
where.And(f => f.Type == newsType);
where.And(f => f.PublishDate <= dtNow);
where.And(f => f.OfflineDate > dtNow);
OrderByClauseBuilder<NewsMaster> sortClauseBuilder = new OrderByClauseBuilder<NewsMaster>();
sortClauseBuilder.Desc(f => f.IsHot);
sortClauseBuilder.Desc(f => f.PublishDate);
sortClauseBuilder.Desc(f => f.EditDate);
return Persister<NewsMaster>.Query(where, sortClauseBuilder, pageSize, pageIndex, out totalCount);
或
WhereClauseBuilder<NewsDetail> where = new WhereClauseBuilder<NewsDetail>();
where.And(f => f.Status == 3);
OrderByClauseBuilder<NewsDetail> sortClauseBuilder = new OrderByClauseBuilder<NewsDetail>();
sortClauseBuilder.Desc(f => f.IsHot);
sortClauseBuilder.Desc(f => f.PublishDate);
return Persister<NewsDetail>.Query(where, sortClauseBuilder);

3】创建一个对象
Persister<AllianceRequest>.Insert<int>(item);

4】查询所有的
return Persister<Area>.QueryAll();

5】使用固定的字符串连接DefaultConnectionString

6】更新
Persister<AllianceRequest>.Update(item);

7】删除
AllianceRequest entity = new AllianceRequest { SysNo = pkid, Status = -1 };
Persister<AllianceRequest>.Update(entity, f => new { f.SysNo, f.Status});

8】自定义的查询
string sql = string.Empty;
if (!string.IsNullOrEmpty(pCode))
{
sql += "SELECT TOP(1) [CategoryCode] FROM [dbo].[ProductCategory] WHERE CategoryCode like @pcode+'%' and [Status]!='D' ";
}
else
{
sql += "SELECT TOP(1) [CategoryCode] FROM [dbo].[ProductCategory] WHERE LEN(CategoryCode)=2 and [Status]!='D' ";
}
sql += " order by CategoryCode desc ";
string code = SqlHelper.ExecuteScalar<string>(CONSTValue.DBConnString, CommandType.Text, sql, new SqlParameter("@pcode", pCode));

 


string sql = @"
SELECT [CategoryCode],[CategoryName],[Status] from 
(
SELECT ROW_NUMBER() OVER(ORDER BY p.CategoryCode asc ) AS RowNumber
,p.CategoryCode,p.CategoryName,p.[Status]
from [dbo].[ProductCategory] p WITH(NOLOCK) {0} 
) t2 WHERE t2.RowNumber Between {1} and {2} 
SELECT @TotalCount=count(1) FROM [dbo].[ProductCategory] p WITH(NOLOCK) {0} ";

totalCount = 0;

List<SqlParameter> listPara = new List<SqlParameter>();
SqlParameter outPara = new SqlParameter("@TotalCount", totalCount);
outPara.SqlDbType = SqlDbType.Int;
outPara.Direction = ParameterDirection.InputOutput;
listPara.Add(outPara);

List<string> listWhere = new List<string>();


if (!string.IsNullOrEmpty(status))
{
listWhere.Add("p.[Status]=@pstatus");
listPara.Add(new SqlParameter("@pstatus", status));
}
if (isChild)
{
listWhere.Add(" len(p.[CategoryCode])=4 ");
}

string where = string.Empty;
if (listWhere.Count != 0)
{
where = " where " + string.Join(" and ", listWhere.ToArray());
}

sql = string.Format(sql, where, (pageIndex - 1) * pageSize + 1, pageSize * pageIndex);

List<ProductCategory> list = new List<ProductCategory>();
using (DataTable objDt = SqlHelper.ExecuteDataTable(CONSTValue.DBConnString, CommandType.Text, sql, listPara.ToArray()))
{
ProductCategory entity = null;
foreach (DataRow dr in objDt.Rows)
{
if (dr["CategoryCode"] == DBNull.Value)
{
continue;
}
entity = new ProductCategory();
entity.CategoryName = dr["CategoryName"].ToString();
list.Add(entity);
}
totalCount = (int)outPara.Value;
}

  

你可能感兴趣的:(使用)