111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
/*******************************************************************************
|
||
* Copyright © 2016 HT.Cloud.Framework 版权所有
|
||
* Author: HT.Cloud
|
||
* Description: WaterCloud快速开发平台
|
||
* Website:
|
||
*********************************************************************************/
|
||
|
||
using System;
|
||
using System.Data;
|
||
using System.Text;
|
||
|
||
namespace HT.Cloud.Code
|
||
{
|
||
public static partial class Extensions
|
||
{
|
||
/// <summary>
|
||
/// 获取表里某页的数据
|
||
/// </summary>
|
||
/// <param name="data">表数据</param>
|
||
/// <param name="pageIndex">当前页</param>
|
||
/// <param name="pageSize">分页大小</param>
|
||
/// <param name="allPage">返回总页数</param>
|
||
/// <returns>返回当页表数据</returns>
|
||
public static DataTable GetPage(this DataTable data, int pageIndex, int pageSize, out int allPage)
|
||
{
|
||
allPage = data.Rows.Count / pageSize;
|
||
allPage += data.Rows.Count % pageSize == 0 ? 0 : 1;
|
||
DataTable Ntable = data.Clone();
|
||
int startIndex = pageIndex * pageSize;
|
||
int endIndex = startIndex + pageSize > data.Rows.Count ? data.Rows.Count : startIndex + pageSize;
|
||
if (startIndex < endIndex)
|
||
for (int i = startIndex; i < endIndex; i++)
|
||
{
|
||
Ntable.ImportRow(data.Rows[i]);
|
||
}
|
||
return Ntable;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据字段过滤表的内容
|
||
/// </summary>
|
||
/// <param name="data">表数据</param>
|
||
/// <param name="condition">条件</param>
|
||
/// <returns></returns>
|
||
///
|
||
public static DataTable GetDataFilter(DataTable data, string condition)
|
||
{
|
||
if (data != null && data.Rows.Count > 0)
|
||
{
|
||
if (condition.Trim() == "")
|
||
{
|
||
return data;
|
||
}
|
||
else
|
||
{
|
||
DataTable newdt = new DataTable();
|
||
newdt = data.Clone();
|
||
DataRow[] dr = data.Select(condition);
|
||
for (int i = 0; i < dr.Length; i++)
|
||
{
|
||
newdt.ImportRow((DataRow)dr[i]);
|
||
}
|
||
return newdt;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 转换为json字符串
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
///
|
||
public static string ToJson(this DataTable dt)
|
||
{
|
||
StringBuilder jsonString = new StringBuilder();
|
||
jsonString.Append("[");
|
||
DataRowCollection drc = dt.Rows;
|
||
for (int i = 0; i < drc.Count; i++)
|
||
{
|
||
jsonString.Append("{");
|
||
for (int j = 0; j < dt.Columns.Count; j++)
|
||
{
|
||
string strKey = dt.Columns[j].ColumnName;
|
||
string strValue = drc[i][j].ToString();
|
||
Type type = dt.Columns[j].DataType;
|
||
jsonString.Append("\"" + strKey + "\":");
|
||
strValue = String.Format(strValue, type);
|
||
if (j < dt.Columns.Count - 1)
|
||
{
|
||
jsonString.Append(strValue + ",");
|
||
}
|
||
else
|
||
{
|
||
jsonString.Append(strValue);
|
||
}
|
||
}
|
||
jsonString.Append("},");
|
||
}
|
||
if (jsonString.Length > 1)
|
||
{
|
||
jsonString.Remove(jsonString.Length - 1, 1);
|
||
}
|
||
jsonString.Append("]");
|
||
return jsonString.ToString();
|
||
}
|
||
}
|
||
} |