1807 lines
59 KiB
C#
1807 lines
59 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Data.SqlClient;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using System.Xml;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Data.Common;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using CYQ.Data.UI;
|
|||
|
|
using CYQ.Data.Cache;
|
|||
|
|
using CYQ.Data.SQL;
|
|||
|
|
using CYQ.Data.Tool;
|
|||
|
|
using System.Collections.Specialized;
|
|||
|
|
using System.Web;
|
|||
|
|
namespace CYQ.Data.Table
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class MDataTable
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD>ʽת<EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>DataReader<65><72>ʽת<CABD><D7AA><EFBFBD><EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
public static implicit operator MDataTable(DbDataReader sdr)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = CreateFrom(sdr);
|
|||
|
|
if (sdr != null)
|
|||
|
|
{
|
|||
|
|
sdr.Close();
|
|||
|
|
sdr.Dispose();
|
|||
|
|
sdr = null;
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>DataTable<6C><65>ʽת<CABD><D7AA><EFBFBD><EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
public static implicit operator MDataTable(DataTable dt)
|
|||
|
|
{
|
|||
|
|
if (dt == null)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
MDataTable mdt = new MDataTable(dt.TableName);
|
|||
|
|
if (dt.Columns != null && dt.Columns.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (DataColumn item in dt.Columns)
|
|||
|
|
{
|
|||
|
|
mdt.Columns.Add(new MCellStruct(item.ColumnName, DataType.GetSqlType(item.DataType), item.ReadOnly, item.AllowDBNull, item.MaxLength));
|
|||
|
|
}
|
|||
|
|
foreach (DataRow row in dt.Rows)
|
|||
|
|
{
|
|||
|
|
MDataRow mdr = mdt.NewRow();
|
|||
|
|
for (int i = 0; i < dt.Columns.Count; i++)
|
|||
|
|
{
|
|||
|
|
mdr[i].Value = row[i];
|
|||
|
|
}
|
|||
|
|
mdt.Rows.Add(mdr, row.RowState != DataRowState.Modified);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return mdt;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>ʽת<CABD><D7AA><EFBFBD><EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="rows"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static implicit operator MDataTable(List<MDataRow> rows)
|
|||
|
|
{
|
|||
|
|
return (MDataRowCollection)rows;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>س<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static implicit operator MDataTable(MDataRow row)
|
|||
|
|
{
|
|||
|
|
MDataTable mTable = new MDataTable(row.TableName);
|
|||
|
|
mTable.Conn = row.Conn;
|
|||
|
|
mTable.LoadRow(row);
|
|||
|
|
return mTable;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>س<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static implicit operator MDataTable(MDataRowCollection rows)
|
|||
|
|
{
|
|||
|
|
if (rows == null || rows.Count == 0)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
MDataTable mdt = new MDataTable(rows[0].TableName);
|
|||
|
|
mdt.Conn = rows[0].Conn;
|
|||
|
|
mdt.Columns = rows[0].Columns;
|
|||
|
|
mdt.Rows.AddRange(rows);
|
|||
|
|
return mdt;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private MDataRowCollection _Rows;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataRowCollection Rows
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Rows;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
[NonSerialized]
|
|||
|
|
private object _DynamicData;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>̬<EFBFBD>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public object DynamicData
|
|||
|
|
{
|
|||
|
|
get { return _DynamicData; }
|
|||
|
|
set { _DynamicData = value; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public MDataTable()
|
|||
|
|
{
|
|||
|
|
Init("default", null);
|
|||
|
|
}
|
|||
|
|
public MDataTable(string tableName)
|
|||
|
|
{
|
|||
|
|
Init(tableName, null);
|
|||
|
|
}
|
|||
|
|
public MDataTable(string tableName, MDataColumn mdc)
|
|||
|
|
{
|
|||
|
|
Init(tableName, mdc);
|
|||
|
|
}
|
|||
|
|
private void Init(string tableName, MDataColumn mdc)
|
|||
|
|
{
|
|||
|
|
_Rows = new MDataRowCollection(this);
|
|||
|
|
_TableName = tableName;
|
|||
|
|
if (_Columns == null)
|
|||
|
|
{
|
|||
|
|
_Columns = new MDataColumn(this);
|
|||
|
|
if (mdc != null)
|
|||
|
|
{
|
|||
|
|
_Columns.AddRange(mdc);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private string _TableName = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string TableName
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _TableName;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_TableName = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private string _Description = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string Description
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(_Description))
|
|||
|
|
{
|
|||
|
|
_Description = TableSchema.GetTableDescription(Conn, _TableName);
|
|||
|
|
if (string.IsNullOrEmpty(_Description))
|
|||
|
|
{
|
|||
|
|
_Description = _TableName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return _Description;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_Description = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private MDataColumn _Columns;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļܹ<C4BC><DCB9><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataColumn Columns
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Columns;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_Columns = value;
|
|||
|
|
_Columns._Table = this;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private string _Conn;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string Conn
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(_Conn))
|
|||
|
|
{
|
|||
|
|
return AppConfig.DB.DefaultConn;
|
|||
|
|
}
|
|||
|
|
return _Conn;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_Conn = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>½<EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataRow NewRow()
|
|||
|
|
{
|
|||
|
|
return NewRow(false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>½<EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="isAddToTable"><3E>Ƿ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataRow NewRow(bool isAddToTable)
|
|||
|
|
{
|
|||
|
|
MDataRow mdr = new MDataRow(this);
|
|||
|
|
if (isAddToTable)
|
|||
|
|
{
|
|||
|
|
Rows.Add(mdr, false);
|
|||
|
|
}
|
|||
|
|
return mdr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼ<EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>¡<EFBFBD><C2A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataTable Select(object where)
|
|||
|
|
{
|
|||
|
|
return Select(0, 0, where);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>¡<EFBFBD><C2A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataTable Select(int topN, object where)
|
|||
|
|
{
|
|||
|
|
return Select(1, topN, where);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>¡<EFBFBD><C2A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataTable Select(int pageIndex, int pageSize, object where, params object[] selectColumns)
|
|||
|
|
{
|
|||
|
|
return MDataTableFilter.Select(this, pageIndex, pageSize, where, selectColumns);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>õ<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD>á<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataRow FindRow(object where)
|
|||
|
|
{
|
|||
|
|
return MDataTableFilter.FindRow(this, where);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>õ<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD>á<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataRowCollection FindAll(object where)
|
|||
|
|
{
|
|||
|
|
return MDataTableFilter.FindAll(this, where);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ͳ<><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public int GetIndex(object where)
|
|||
|
|
{
|
|||
|
|
return MDataTableFilter.GetIndex(this, where);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ͳ<><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public int GetCount(object where)
|
|||
|
|
{
|
|||
|
|
return MDataTableFilter.GetCount(this, where);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ġ<EFBFBD><C4A1><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬһ<CDAC><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataTable[] Split(object where)
|
|||
|
|
{
|
|||
|
|
return MDataTableFilter.Split(this, where);
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD>мܹ<D0BC>)[<5B><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ռܹ<D5BC>ʱ<EFBFBD><CAB1>Ч]
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="row"></param>
|
|||
|
|
internal void LoadRow(MDataRow row) //<2F>Ƿ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Row.Table<6C>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (this.Columns.Count == 0 && row != null && row.Count > 0)
|
|||
|
|
{
|
|||
|
|
this.Columns = row.Columns.Clone();
|
|||
|
|
if (!string.IsNullOrEmpty(_TableName) && _TableName.StartsWith("SysDefault"))
|
|||
|
|
{
|
|||
|
|
_TableName = row.TableName;
|
|||
|
|
}
|
|||
|
|
_Conn = row.Conn;
|
|||
|
|
if (!row[0].IsNullOrEmpty)
|
|||
|
|
{
|
|||
|
|
NewRow(true).LoadFrom(row);
|
|||
|
|
//_Rows.Add(row);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ת<><D7AA><EFBFBD><EFBFBD>DataTable
|
|||
|
|
/// </summary>
|
|||
|
|
public DataTable ToDataTable()
|
|||
|
|
{
|
|||
|
|
DataTable dt = new DataTable(_TableName);
|
|||
|
|
if (Columns != null && Columns.Count > 0)
|
|||
|
|
{
|
|||
|
|
bool checkDuplicate = Columns.CheckDuplicate;
|
|||
|
|
List<string> duplicateName = new List<string>();
|
|||
|
|
for (int j = 0; j < Columns.Count; j++)
|
|||
|
|
{
|
|||
|
|
MCellStruct item = Columns[j];
|
|||
|
|
if (string.IsNullOrEmpty(item.ColumnName))
|
|||
|
|
{
|
|||
|
|
item.ColumnName = "Empty_" + item;
|
|||
|
|
}
|
|||
|
|
if (!checkDuplicate && dt.Columns.Contains(item.ColumnName))//ȥ<>ء<EFBFBD>
|
|||
|
|
{
|
|||
|
|
string rndName = Guid.NewGuid().ToString();
|
|||
|
|
dt.Columns.Add(rndName, item.ValueType);
|
|||
|
|
duplicateName.Add(rndName);
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
dt.Columns.Add(item.ColumnName, item.ValueType);
|
|||
|
|
}
|
|||
|
|
int count = dt.Columns.Count;
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
DataRow dr = dt.NewRow();
|
|||
|
|
for (int i = 0; i < count; i++)
|
|||
|
|
{
|
|||
|
|
if (row[i].IsNull)
|
|||
|
|
{
|
|||
|
|
dr[i] = DBNull.Value;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
dr[i] = row[i].Value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dt.Rows.Add(dr);
|
|||
|
|
}
|
|||
|
|
for (int i = 0; i < duplicateName.Count; i++)
|
|||
|
|
{
|
|||
|
|
dt.Columns.Remove(duplicateName[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
dt.AcceptChanges();
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Xml<6D>ĵ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string ToXml()
|
|||
|
|
{
|
|||
|
|
return ToXml(false);
|
|||
|
|
}
|
|||
|
|
public string ToXml(bool isConvertNameToLower)
|
|||
|
|
{
|
|||
|
|
return ToXml(isConvertNameToLower, true, true);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Xml<6D>ĵ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="isConvertNameToLower"><3E><><EFBFBD><EFBFBD>תСд</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public string ToXml(bool isConvertNameToLower, bool needHeader, bool needRootNode)
|
|||
|
|
{
|
|||
|
|
StringBuilder xml = new StringBuilder();
|
|||
|
|
if (Columns.Count > 0)
|
|||
|
|
{
|
|||
|
|
string tableName = string.IsNullOrEmpty(_TableName) ? "Root" : _TableName;
|
|||
|
|
string rowName = string.IsNullOrEmpty(_TableName) ? "Row" : _TableName;
|
|||
|
|
if (isConvertNameToLower)
|
|||
|
|
{
|
|||
|
|
tableName = tableName.ToLower();
|
|||
|
|
rowName = rowName.ToLower();
|
|||
|
|
}
|
|||
|
|
if (needHeader)
|
|||
|
|
{
|
|||
|
|
xml.Append("<?xml version=\"1.0\" standalone=\"yes\"?>");
|
|||
|
|
}
|
|||
|
|
if (needRootNode)
|
|||
|
|
{
|
|||
|
|
xml.AppendFormat("\r\n<{0}>", tableName);
|
|||
|
|
}
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
xml.AppendFormat("\r\n <{0}>", rowName);
|
|||
|
|
foreach (MDataCell cell in row)
|
|||
|
|
{
|
|||
|
|
xml.Append(cell.ToXml(isConvertNameToLower));
|
|||
|
|
}
|
|||
|
|
xml.AppendFormat("\r\n </{0}>", rowName);
|
|||
|
|
}
|
|||
|
|
if (needRootNode)
|
|||
|
|
{
|
|||
|
|
xml.AppendFormat("\r\n</{0}>", tableName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return xml.ToString();
|
|||
|
|
}
|
|||
|
|
public bool WriteXml(string fileName)
|
|||
|
|
{
|
|||
|
|
return WriteXml(fileName, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Xml
|
|||
|
|
/// </summary>
|
|||
|
|
public bool WriteXml(string fileName, bool isConvertNameToLower)
|
|||
|
|
{
|
|||
|
|
return IOHelper.Write(fileName, ToXml(isConvertNameToLower), Encoding.UTF8);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Json
|
|||
|
|
/// </summary>
|
|||
|
|
public string ToJson()
|
|||
|
|
{
|
|||
|
|
return ToJson(true);
|
|||
|
|
}
|
|||
|
|
public string ToJson(bool addHead)
|
|||
|
|
{
|
|||
|
|
return ToJson(addHead, false);
|
|||
|
|
}
|
|||
|
|
/// <param name="addHead"><3E><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>Ϣ[<5B><>count<6E><74>Success<73><73>ErrorMsg](Ĭ<><C4AC>true)</param>
|
|||
|
|
/// <param name="addSchema"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD>Ϣ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ɻ<EFBFBD>ԭ<EFBFBD>ܹ<EFBFBD>(Ĭ<><C4AC>false)</param>
|
|||
|
|
public string ToJson(bool addHead, bool addSchema)
|
|||
|
|
{
|
|||
|
|
return ToJson(addHead, addSchema, RowOp.None);
|
|||
|
|
}
|
|||
|
|
/// <param name="rowOp"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
public string ToJson(bool addHead, bool addSchema, RowOp rowOp)
|
|||
|
|
{
|
|||
|
|
return ToJson(addHead, addSchema, rowOp, false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string ToJson(bool addHead, bool addSchema, RowOp rowOp, bool isConvertNameToLower)
|
|||
|
|
{
|
|||
|
|
return ToJson(addHead, addSchema, rowOp, isConvertNameToLower, JsonHelper.DefaultEscape);
|
|||
|
|
}
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
public string ToJson(bool addHead, bool addSchema, RowOp rowOp, bool isConvertNameToLower, EscapeOp escapeOp)
|
|||
|
|
{
|
|||
|
|
JsonHelper helper = new JsonHelper(addHead, addSchema);
|
|||
|
|
helper.Escape = escapeOp;
|
|||
|
|
helper.IsConvertNameToLower = isConvertNameToLower;
|
|||
|
|
helper.RowOp = rowOp;
|
|||
|
|
helper.Fill(this);
|
|||
|
|
bool checkArrayEnd = !addHead && !addSchema;
|
|||
|
|
return helper.ToString(checkArrayEnd);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Json[<5B><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>]
|
|||
|
|
/// </summary>
|
|||
|
|
public bool WriteJson(bool addHead, bool addSchema, string fileName)
|
|||
|
|
{
|
|||
|
|
return IOHelper.Write(fileName, ToJson(addHead, addSchema));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><F3B6A8B5>б<EFBFBD><D0B1>ؼ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="control"><3E>б<EFBFBD><D0B1>ؼ<EFBFBD>[<5B><><EFBFBD><EFBFBD>Repeater/DataList/GridView/DataGrid<69><64>]</param>
|
|||
|
|
public void Bind(object control)
|
|||
|
|
{
|
|||
|
|
Bind(control, null);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><F3B6A8B5>б<EFBFBD><D0B1>ؼ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="control"><3E>б<EFBFBD><D0B1>ؼ<EFBFBD>[<5B><><EFBFBD><EFBFBD>Repeater/DataList/GridView/DataGrid<69><64>]</param>
|
|||
|
|
/// <param name="nodeID"><3E><>ControlΪXHtmlAction<6F><6E><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫָ<D2AA><D6B8><EFBFBD>Ľڵ<C4BD>ID</param>
|
|||
|
|
public void Bind(object control, string nodeID)
|
|||
|
|
{
|
|||
|
|
MBindUI.Bind(control, this, nodeID);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>±<EFBFBD><C2B1><EFBFBD><EFBFBD>зŵ<D0B7>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>档
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="newTable"></param>
|
|||
|
|
public void Merge(MDataTable newTable)
|
|||
|
|
{
|
|||
|
|
if (newTable != null && newTable.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
int count = newTable.Rows.Count;//<2F><>ǰ<EFBFBD><C7B0>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˱<EFBFBD><CBB1><EFBFBD>dt.Merge(dt);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>
|
|||
|
|
for (int i = 0; i < count; i++)
|
|||
|
|
{
|
|||
|
|
// _Rows.Add(newTable.Rows[i]);
|
|||
|
|
NewRow(true).LoadFrom(newTable.Rows[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>״̬ȫ<CCAC><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="state">״̬[0:δ<><CEB4><EFBFBD>ģ<EFBFBD>1:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɲ<EFBFBD><C9B2><EFBFBD>]<5D><>2:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɸ<EFBFBD><C9B8><EFBFBD>]]</param>
|
|||
|
|
public MDataTable SetState(int state)
|
|||
|
|
{
|
|||
|
|
SetState(state, BreakOp.None); return this;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>״̬ȫ<CCAC><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="state">״̬[0:δ<><CEB4><EFBFBD>ģ<EFBFBD>1:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɲ<EFBFBD><C9B2><EFBFBD>]<5D><>2:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɸ<EFBFBD><C9B8><EFBFBD>]]</param>
|
|||
|
|
/// <param name="op">״̬<D7B4><CCAC><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
public MDataTable SetState(int state, BreakOp op)
|
|||
|
|
{
|
|||
|
|
if (Rows != null && Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
row.SetState(state, op);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return this;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// תʵ<D7AA><CAB5><EFBFBD>б<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="useEmit"><3E>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9>Emit<69><74>ʽת<CABD><D7AA>[<5B><><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>[<5B><><EFBFBD><EFBFBD>500<30><30>]<5D><><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>],<2C><>дĬ<D0B4><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ж<EFBFBD></param>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public List<T> ToList<T>(params bool[] useEmit)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
List<T> list = new List<T>();
|
|||
|
|
if (Rows != null && Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
if (((Rows.Count > 500 && useEmit.Length == 0) || (useEmit.Length > 0 && useEmit[0])) && typeof(T).BaseType.Name != "OrmBase")//Զ<>̴<EFBFBD><CCB4><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB>䣬<EFBFBD><EFBFBD><DEB7><EFBFBD>Emit
|
|||
|
|
{
|
|||
|
|
FastToT<T>.EmitHandle emit = FastToT<T>.Create(this);
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
list.Add(emit(row));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
list.Add(row.ToEntity<T>());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
internal object ToList(Type t)
|
|||
|
|
{
|
|||
|
|
object listObj = Activator.CreateInstance(t);//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
|||
|
|
if (Rows != null && Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
Type[] paraTypeList = null;
|
|||
|
|
Type listObjType=listObj.GetType();
|
|||
|
|
StaticTool.GetArgumentLength(ref listObjType, out paraTypeList);
|
|||
|
|
MethodInfo method = listObjType.GetMethod("Add");
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
method.Invoke(listObj, new object[] { row.ToEntity(paraTypeList[0]) });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return listObj;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [<5B><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>йأ<D0B9><D8A3>統ǰ<E7B5B1><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ύ<EFBFBD><E1BDBB><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>,<2C><><EFBFBD><EFBFBD>TableName<6D><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¸<EFBFBD>ֵ]
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>[<5B><><EFBFBD><EFBFBD>|<7C><><EFBFBD><EFBFBD>]</param>
|
|||
|
|
public bool AcceptChanges(AcceptOp op)
|
|||
|
|
{
|
|||
|
|
return AcceptChanges(op, string.Empty);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>[<5B><><EFBFBD><EFBFBD>|<7C><><EFBFBD><EFBFBD>]</param>
|
|||
|
|
/// <param name="newConn">ָ<><D6B8><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="jointPrimaryKeys">AcceptOpΪUpdate<74><65>Autoʱ<6F><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪΨһ<CEA8><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD></param>
|
|||
|
|
public bool AcceptChanges(AcceptOp op, string newConn, params object[] jointPrimaryKeys)
|
|||
|
|
{
|
|||
|
|
bool result = false;
|
|||
|
|
if (Columns.Count == 0 || Rows.Count == 0)
|
|||
|
|
{
|
|||
|
|
return false;//ľ<>пɸ<D0BF><C9B8>µġ<C2B5>
|
|||
|
|
}
|
|||
|
|
MDataTableBatchAction action = new MDataTableBatchAction(this, newConn);
|
|||
|
|
if ((op & AcceptOp.Truncate) != 0)
|
|||
|
|
{
|
|||
|
|
action.IsTruncate = true;
|
|||
|
|
op = (AcceptOp)(op - AcceptOp.Truncate);
|
|||
|
|
}
|
|||
|
|
action.SetJoinPrimaryKeys(jointPrimaryKeys);
|
|||
|
|
switch (op)
|
|||
|
|
{
|
|||
|
|
case AcceptOp.Insert:
|
|||
|
|
result = action.Insert(false);
|
|||
|
|
break;
|
|||
|
|
case AcceptOp.InsertWithID:
|
|||
|
|
result = action.Insert(true);
|
|||
|
|
break;
|
|||
|
|
case AcceptOp.Update:
|
|||
|
|
result = action.Update();
|
|||
|
|
break;
|
|||
|
|
case AcceptOp.Delete:
|
|||
|
|
result = action.Delete();
|
|||
|
|
break;
|
|||
|
|
case AcceptOp.Auto:
|
|||
|
|
result = action.Auto();
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
if (result && AppConfig.Cache.IsAutoCache)
|
|||
|
|
{
|
|||
|
|
//ȡ<><C8A1>AOP<4F><50><EFBFBD>档
|
|||
|
|
AutoCache.ReadyForRemove(AutoCache.GetBaseKey(action.dalTypeTo, action.database, TableName));
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>Ĺ<DEB8><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataTable GetChanges()
|
|||
|
|
{
|
|||
|
|
return GetChanges(RowOp.Update);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>Ĺ<DEB8><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD>ģ<DEB8><C4A3><EFBFBD>Null<6C><6C>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="rowOp"><3E><>Insert<72><74>Updateѡ<65><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataTable GetChanges(RowOp rowOp)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = new MDataTable(_TableName);
|
|||
|
|
dt.Columns = Columns;
|
|||
|
|
dt.Conn = Conn;
|
|||
|
|
dt.DynamicData = DynamicData;
|
|||
|
|
dt.joinOnIndex = joinOnIndex;
|
|||
|
|
dt.JoinOnName = dt.JoinOnName;
|
|||
|
|
dt.RecordsAffected = RecordsAffected;
|
|||
|
|
if (this.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
if (rowOp == RowOp.Insert || rowOp == RowOp.Update)
|
|||
|
|
{
|
|||
|
|
int stateValue = (int)rowOp;
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
if (row.GetState() >= stateValue)
|
|||
|
|
{
|
|||
|
|
dt.Rows.Add(row, false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>еļ<D0B5><C4BC><EFBFBD>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// </summary>
|
|||
|
|
public List<T> GetColumnItems<T>(string columnName)
|
|||
|
|
{
|
|||
|
|
return GetColumnItems<T>(columnName, BreakOp.None, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>еļ<D0B5><C4BC><EFBFBD>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
/// </summary>
|
|||
|
|
public List<T> GetColumnItems<T>(string columnName, BreakOp op)
|
|||
|
|
{
|
|||
|
|
return GetColumnItems<T>(columnName, op, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>еļ<D0B5><C4BC><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnIndex"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
/// <param name="isDistinct"><3E>Ƿ<EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
public List<T> GetColumnItems<T>(string columnName, BreakOp op, bool isDistinct)
|
|||
|
|
{
|
|||
|
|
int index = -1;
|
|||
|
|
if (Columns != null)
|
|||
|
|
{
|
|||
|
|
index = Columns.GetIndex(columnName);
|
|||
|
|
}
|
|||
|
|
return GetColumnItems<T>(index, op, isDistinct);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>еļ<D0B5><C4BC><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnIndex"><3E><>N<EFBFBD><4E></param>
|
|||
|
|
public List<T> GetColumnItems<T>(int columnIndex)
|
|||
|
|
{
|
|||
|
|
return GetColumnItems<T>(columnIndex, BreakOp.None);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>еļ<D0B5><C4BC><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnIndex"><3E><>N<EFBFBD><4E></param>
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public List<T> GetColumnItems<T>(int columnIndex, BreakOp op)
|
|||
|
|
{
|
|||
|
|
return GetColumnItems<T>(columnIndex, op, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>еļ<D0B5><C4BC><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnIndex"><3E><>N<EFBFBD><4E></param>
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
/// <param name="isDistinct"><3E>Ƿ<EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
public List<T> GetColumnItems<T>(int columnIndex, BreakOp op, bool isDistinct)
|
|||
|
|
{
|
|||
|
|
List<T> items = new List<T>();
|
|||
|
|
if (Columns != null && Rows != null && Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
if (columnIndex > -1)
|
|||
|
|
{
|
|||
|
|
MDataCell cell;
|
|||
|
|
foreach (MDataRow row in Rows)
|
|||
|
|
{
|
|||
|
|
cell = row[columnIndex];
|
|||
|
|
switch (op)
|
|||
|
|
{
|
|||
|
|
case BreakOp.Null:
|
|||
|
|
if (cell.IsNull)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case BreakOp.Empty:
|
|||
|
|
if (cell.StringValue == "")
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case BreakOp.NullOrEmpty:
|
|||
|
|
if (cell.IsNullOrEmpty)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
T value = row.Get<T>(columnIndex, default(T));
|
|||
|
|
if (!isDistinct || !items.Contains(value))
|
|||
|
|
{
|
|||
|
|
items.Add(value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Error.Throw(string.Format("Table {0} can not find the column", TableName));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return items;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ʊ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataTable Clone()
|
|||
|
|
{
|
|||
|
|
MDataTable newTable = GetSchema(true);
|
|||
|
|
newTable.Conn = Conn;
|
|||
|
|
newTable.DynamicData = DynamicData;
|
|||
|
|
newTable.RecordsAffected = RecordsAffected;
|
|||
|
|
newTable.TableName = TableName;
|
|||
|
|
if (_Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (MDataRow oldRow in _Rows)
|
|||
|
|
{
|
|||
|
|
MDataRow newRow = newTable.NewRow();
|
|||
|
|
newRow.LoadFrom(oldRow);
|
|||
|
|
newTable.Rows.Add(newRow, false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return newTable;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ʊ<EFBFBD><C6B1>Ľṹ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="clone"><3E>Ƿ<EFBFBD><C7B7><EFBFBD>¡<EFBFBD><C2A1><EFBFBD>ṹ</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataTable GetSchema(bool clone)
|
|||
|
|
{
|
|||
|
|
MDataTable newTable = new MDataTable(_TableName);
|
|||
|
|
if (Columns.Count > 0)
|
|||
|
|
{
|
|||
|
|
newTable.Columns = clone ? Columns.Clone() : Columns;
|
|||
|
|
}
|
|||
|
|
newTable.Conn = Conn;
|
|||
|
|
return newTable;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>˵<EFBFBD><CBB5>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>Ƚϻ<C8BD><CFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD><CDA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><CDBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>
|
|||
|
|
/// <param name="filterRows"><3E><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD></param>
|
|||
|
|
/// </summary>
|
|||
|
|
public void Distinct(out MDataTable filterRows)
|
|||
|
|
{
|
|||
|
|
filterRows = null;
|
|||
|
|
if (Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
List<MDataRow> rowList = new List<MDataRow>();
|
|||
|
|
int cCount = Columns.Count;
|
|||
|
|
for (int i = 0; i < Rows.Count; i++)
|
|||
|
|
{
|
|||
|
|
for (int j = Rows.Count - 1; j >= 0 && j != i; j--)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⡣
|
|||
|
|
{
|
|||
|
|
int eqCount = 0;
|
|||
|
|
for (int k = 0; k < cCount; k++)//<2F>Ƚ<EFBFBD><C8BD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (Rows[i][k].StringValue == Rows[j][k].StringValue)
|
|||
|
|
{
|
|||
|
|
eqCount++;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (eqCount == cCount)
|
|||
|
|
{
|
|||
|
|
rowList.Add(Rows[j]);
|
|||
|
|
Rows.RemoveAt(j);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (rowList.Count > 0)
|
|||
|
|
{
|
|||
|
|
filterRows = rowList;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>˵<EFBFBD><CBB5>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>Ƚϻ<C8BD><CFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD><CDA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><CDBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public void Distinct()
|
|||
|
|
{
|
|||
|
|
MDataTable filterRows;
|
|||
|
|
Distinct(out filterRows);
|
|||
|
|
filterRows = null;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
public override string ToString()
|
|||
|
|
{
|
|||
|
|
return TableName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class MDataTable : IDataReader, IEnumerable//, IEnumerator
|
|||
|
|
{
|
|||
|
|
private int _Ptr = -1;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
#region IDataRecord <EFBFBD><EFBFBD>Ա
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
int IDataRecord.FieldCount
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (Columns != null)
|
|||
|
|
{
|
|||
|
|
return Columns.Count;
|
|||
|
|
}
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool IDataRecord.GetBoolean(int i)
|
|||
|
|
{
|
|||
|
|
return (bool)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
byte IDataRecord.GetByte(int i)
|
|||
|
|
{
|
|||
|
|
return (byte)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
long IDataRecord.GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
|
|||
|
|
{
|
|||
|
|
return (byte)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
char IDataRecord.GetChar(int i)
|
|||
|
|
{
|
|||
|
|
return (char)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
long IDataRecord.GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
|
|||
|
|
{
|
|||
|
|
return (char)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IDataReader IDataRecord.GetData(int i)
|
|||
|
|
{
|
|||
|
|
return this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string IDataRecord.GetDataTypeName(int i)
|
|||
|
|
{
|
|||
|
|
return "";//<2F>Ŀ<F3B6A8B5><C4BF>Բ<EFBFBD><D4B2><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
|||
|
|
//return DataType.GetDbType(Columns[i].SqlType.ToString()).ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DateTime IDataRecord.GetDateTime(int i)
|
|||
|
|
{
|
|||
|
|
return (DateTime)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
decimal IDataRecord.GetDecimal(int i)
|
|||
|
|
{
|
|||
|
|
return (decimal)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
double IDataRecord.GetDouble(int i)
|
|||
|
|
{
|
|||
|
|
return (double)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Type IDataRecord.GetFieldType(int i)
|
|||
|
|
{
|
|||
|
|
return _Columns[i].ValueType;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
float IDataRecord.GetFloat(int i)
|
|||
|
|
{
|
|||
|
|
return (float)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Guid IDataRecord.GetGuid(int i)
|
|||
|
|
{
|
|||
|
|
return (Guid)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
short IDataRecord.GetInt16(int i)
|
|||
|
|
{
|
|||
|
|
return (short)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int IDataRecord.GetInt32(int i)
|
|||
|
|
{
|
|||
|
|
return (int)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
long IDataRecord.GetInt64(int i)
|
|||
|
|
{
|
|||
|
|
return (long)_Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string IDataRecord.GetName(int i)
|
|||
|
|
{
|
|||
|
|
return _Columns[i].ColumnName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int IDataRecord.GetOrdinal(string name)
|
|||
|
|
{
|
|||
|
|
return _Columns.GetIndex(name);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string IDataRecord.GetString(int i)
|
|||
|
|
{
|
|||
|
|
return Convert.ToString(_Rows[_Ptr][i].Value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
object IDataRecord.GetValue(int i)
|
|||
|
|
{
|
|||
|
|
return _Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int IDataRecord.GetValues(object[] values)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < values.Length; i++)
|
|||
|
|
{
|
|||
|
|
values[i] = _Rows[_Ptr][i].Value;
|
|||
|
|
}
|
|||
|
|
return values.Length;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool IDataRecord.IsDBNull(int i)
|
|||
|
|
{
|
|||
|
|
return _Rows[_Ptr][i].IsNull;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
object IDataRecord.this[string name]
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Rows[_Ptr][name];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
object IDataRecord.this[int i]
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Rows[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IDataReader <EFBFBD><EFBFBD>Ա
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
void IDataReader.Close()
|
|||
|
|
{
|
|||
|
|
_Rows.Clear();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
int IDataReader.Depth
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (_Rows != null)
|
|||
|
|
{
|
|||
|
|
return _Rows.Count;
|
|||
|
|
}
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DataTable IDataReader.GetSchemaTable()
|
|||
|
|
{
|
|||
|
|
return ToDataTable();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7>Ѷ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼<EFBFBD>¼<EFBFBD><C2BC>
|
|||
|
|
/// </summary>
|
|||
|
|
bool IDataReader.IsClosed
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Rows.Count == 0 && _Ptr >= _Rows.Count - 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
bool IDataReader.NextResult()
|
|||
|
|
{
|
|||
|
|
return _Ptr < _Rows.Count - 1;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><C8A1>
|
|||
|
|
/// </summary>
|
|||
|
|
bool IDataReader.Read()
|
|||
|
|
{
|
|||
|
|
if (_Ptr < _Rows.Count - 1)
|
|||
|
|
{
|
|||
|
|
_Ptr++;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_Ptr = -1;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int _RecordsAffected;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>أ<EFBFBD><D8A3><EFBFBD>ѯʱ<D1AF><CAB1><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public int RecordsAffected
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (_RecordsAffected == 0)
|
|||
|
|
{
|
|||
|
|
return _Rows.Count;
|
|||
|
|
}
|
|||
|
|
return _RecordsAffected;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_RecordsAffected = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IDisposable <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
void IDisposable.Dispose()
|
|||
|
|
{
|
|||
|
|
_Rows.Clear();
|
|||
|
|
_Rows = null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IEnumerable <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|||
|
|
{
|
|||
|
|
//for (int i = 0; i < Rows.Count; i++)
|
|||
|
|
//{
|
|||
|
|
// yield return Rows[i];
|
|||
|
|
//}
|
|||
|
|
return new System.Data.Common.DbEnumerator(this);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class MDataTable
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD> CreateFrom
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ر<EFBFBD>Sdr<64><72><EFBFBD><EFBFBD>Ϊ<EFBFBD>ⲿMProc.ExeMDataTableList<73><74><EFBFBD><EFBFBD>Ҫʹ<D2AA>ã<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sdr"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
internal static MDataTable CreateFrom(DbDataReader sdr)
|
|||
|
|
{
|
|||
|
|
MDataTable mTable = new MDataTable("SysDefault");
|
|||
|
|
if (sdr != null && sdr.FieldCount > 0)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
DataTable dt = null;
|
|||
|
|
bool noSchema = OracleDal.clientType == 0;
|
|||
|
|
if (!noSchema)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
dt = sdr.GetSchemaTable();
|
|||
|
|
}
|
|||
|
|
catch { noSchema = true; }
|
|||
|
|
}
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ
|
|||
|
|
if (noSchema) //OracleClient <20><>֧<EFBFBD><D6A7><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD><D1AF>GetSchemaTable<6C><65><EFBFBD><EFBFBD>ODP.NET<45><54>֧<EFBFBD>ֵġ<D6B5>
|
|||
|
|
{
|
|||
|
|
//<2F><>DataReader<65><72>ȡ<EFBFBD><C8A1><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
string hiddenFields = "," + AppConfig.DB.HiddenFields.ToLower() + ",";
|
|||
|
|
MCellStruct mStruct;
|
|||
|
|
for (int i = 0; i < sdr.FieldCount; i++)
|
|||
|
|
{
|
|||
|
|
string name = sdr.GetName(i);
|
|||
|
|
if (string.IsNullOrEmpty(name))
|
|||
|
|
{
|
|||
|
|
name = "Empty_" + i;
|
|||
|
|
}
|
|||
|
|
bool isHiddenField = hiddenFields.IndexOf("," + name + ",", StringComparison.OrdinalIgnoreCase) > -1;
|
|||
|
|
if (!isHiddenField)
|
|||
|
|
{
|
|||
|
|
mStruct = new MCellStruct(name, DataType.GetSqlType(sdr.GetFieldType(i)));
|
|||
|
|
mStruct.ReaderIndex = i;
|
|||
|
|
mTable.Columns.Add(mStruct);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (dt != null && dt.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
TableSchema.FixTableSchemaType(sdr, dt);
|
|||
|
|
mTable.Columns = TableSchema.GetColumns(dt);
|
|||
|
|
mTable.Columns.dalType = DalCreate.GetDalTypeByReaderName(sdr.GetType().Name);
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
if (sdr.HasRows)
|
|||
|
|
{
|
|||
|
|
MDataRow mRecord = null;
|
|||
|
|
List<int> errIndex = new List<int>();//SQLite<74>ṩ<EFBFBD><E1B9A9>dll<6C><6C><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD>sdr[x]<5D><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>쳣
|
|||
|
|
while (sdr.Read())
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mRecord = mTable.NewRow(true);
|
|||
|
|
|
|||
|
|
for (int i = 0; i < mTable.Columns.Count; i++)
|
|||
|
|
{
|
|||
|
|
MCellStruct ms = mTable.Columns[i];
|
|||
|
|
object value = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (errIndex.Contains(i))
|
|||
|
|
{
|
|||
|
|
value = sdr.GetString(ms.ReaderIndex);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
value = sdr[ms.ReaderIndex];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
if (!errIndex.Contains(i))
|
|||
|
|
{
|
|||
|
|
errIndex.Add(i);
|
|||
|
|
}
|
|||
|
|
value = sdr.GetString(ms.ReaderIndex);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (value == null || value == DBNull.Value)
|
|||
|
|
{
|
|||
|
|
mRecord[i].Value = DBNull.Value;
|
|||
|
|
mRecord[i].State = 0;
|
|||
|
|
}
|
|||
|
|
else if (Convert.ToString(value) == string.Empty)
|
|||
|
|
{
|
|||
|
|
mRecord[i].Value = string.Empty;
|
|||
|
|
mRecord[i].State = 1;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
mRecord[i].Value = value; //sdr.GetValue(i); <20>ô˸<C3B4>ֵ<EFBFBD><D6B5><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
|||
|
|
mRecord[i].State = 1;//<2F><>ʼʼ״̬Ϊ1
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return mTable;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>List<73>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="entityList">ʵ<><CAB5><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static MDataTable CreateFrom(object entityList, BreakOp op)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = new MDataTable("SysDefault");
|
|||
|
|
if (entityList != null)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
bool isObj = true;
|
|||
|
|
Type t = entityList.GetType();
|
|||
|
|
|
|||
|
|
if (t.IsGenericType)
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|||
|
|
Type[] types;
|
|||
|
|
int len = StaticTool.GetArgumentLength(ref t, out types);
|
|||
|
|
if (len == 2)//<2F>ֵ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
dt.Columns.Add("Key", DataType.GetSqlType(types[0]));
|
|||
|
|
dt.Columns.Add("Value", DataType.GetSqlType(types[1]));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Type objType = types[0];
|
|||
|
|
if ((objType.FullName.StartsWith("System.") && objType.FullName.Split('.').Length == 2) || objType.IsEnum)//ϵͳ<CFB5><CDB3><EFBFBD>͡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
isObj = false;
|
|||
|
|
string name = objType.Name.Split('`')[0];
|
|||
|
|
if (name.StartsWith("Nullable"))
|
|||
|
|
{
|
|||
|
|
name = Nullable.GetUnderlyingType(objType).Name;
|
|||
|
|
}
|
|||
|
|
dt.Columns.Add(name, DataType.GetSqlType(objType), false);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
dt.TableName = objType.Name;
|
|||
|
|
dt.Columns = TableSchema.GetColumns(objType);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
else if (entityList is Hashtable)
|
|||
|
|
{
|
|||
|
|
dt.Columns.Add("Key");
|
|||
|
|
dt.Columns.Add("Value");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
isObj = false;
|
|||
|
|
dt.Columns.Add(t.Name.Replace("[]", ""), SqlDbType.Variant, false);
|
|||
|
|
}
|
|||
|
|
foreach (object o in entityList as IEnumerable)
|
|||
|
|
{
|
|||
|
|
MDataRow row = dt.NewRow();
|
|||
|
|
if (isObj)
|
|||
|
|
{
|
|||
|
|
row.LoadFrom(o, op);//<2F><>ʼֵ״̬Ϊ1
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
row.Set(0, o, 1);
|
|||
|
|
}
|
|||
|
|
dt.Rows.Add(row);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt(err);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
public static MDataTable CreateFrom(object entityList)
|
|||
|
|
{
|
|||
|
|
return CreateFrom(entityList, BreakOp.None);
|
|||
|
|
}
|
|||
|
|
public static MDataTable CreateFrom(NameObjectCollectionBase noc)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = new MDataTable("SysDefault");
|
|||
|
|
if (noc != null)
|
|||
|
|
{
|
|||
|
|
if (noc is NameValueCollection)
|
|||
|
|
{
|
|||
|
|
dt.Columns.Add("Key");
|
|||
|
|
dt.Columns.Add("Value");
|
|||
|
|
NameValueCollection nv = noc as NameValueCollection;
|
|||
|
|
foreach (string key in nv)
|
|||
|
|
{
|
|||
|
|
dt.NewRow(true).Set(0, key, 1).Set(1, nv[key], 1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (noc is HttpCookieCollection)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
HttpCookieCollection nv = noc as HttpCookieCollection;
|
|||
|
|
dt.Columns.Add("Name");
|
|||
|
|
dt.Columns.Add("Value");
|
|||
|
|
dt.Columns.Add("Expires");
|
|||
|
|
dt.Columns.Add("Domain");
|
|||
|
|
dt.Columns.Add("HttpOnly");
|
|||
|
|
dt.Columns.Add("Path");
|
|||
|
|
for (int i = 0; i < nv.Count; i++)
|
|||
|
|
{
|
|||
|
|
HttpCookie cookie = nv[i];
|
|||
|
|
dt.NewRow(true).Set(0, cookie.Name).Set(1, cookie.Value)
|
|||
|
|
.Set(2, cookie.Expires)
|
|||
|
|
.Set(3, cookie.Domain)
|
|||
|
|
.Set(4, cookie.HttpOnly)
|
|||
|
|
.Set(5, cookie.Path);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
dt = CreateFrom(noc as IEnumerable);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>Json<6F><6E>Xml<6D>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
public static MDataTable CreateFrom(string jsonOrXml)
|
|||
|
|
{
|
|||
|
|
return CreateFrom(jsonOrXml, null);
|
|||
|
|
}
|
|||
|
|
public static MDataTable CreateFrom(string jsonOrXml, MDataColumn mdc)
|
|||
|
|
{
|
|||
|
|
return CreateFrom(jsonOrXml, mdc, JsonHelper.DefaultEscape);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>Json<6F><6E>Xml<6D>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
public static MDataTable CreateFrom(string jsonOrXml, MDataColumn mdc,EscapeOp op)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(jsonOrXml))
|
|||
|
|
{
|
|||
|
|
if (jsonOrXml[0] == '<' || jsonOrXml.EndsWith(".xml"))
|
|||
|
|
{
|
|||
|
|
return CreateFromXml(jsonOrXml, mdc);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return JsonHelper.ToMDataTable(jsonOrXml, mdc,op);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return new MDataTable();
|
|||
|
|
}
|
|||
|
|
internal static MDataTable CreateFromXml(string xmlOrFileName, MDataColumn mdc)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = new MDataTable();
|
|||
|
|
if (mdc != null)
|
|||
|
|
{
|
|||
|
|
dt.Columns = mdc;
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrEmpty(xmlOrFileName))
|
|||
|
|
{
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
xmlOrFileName = xmlOrFileName.Trim();
|
|||
|
|
XmlDocument doc = new XmlDocument();
|
|||
|
|
bool loadOk = false;
|
|||
|
|
if (!xmlOrFileName.StartsWith("<"))//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7>
|
|||
|
|
{
|
|||
|
|
dt.TableName = Path.GetFileNameWithoutExtension(xmlOrFileName);
|
|||
|
|
dt.Columns = MDataColumn.CreateFrom(xmlOrFileName, false);
|
|||
|
|
if (File.Exists(xmlOrFileName))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
doc.Load(xmlOrFileName);
|
|||
|
|
loadOk = true;
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{ }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else // xml <20>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
doc.LoadXml(xmlOrFileName);
|
|||
|
|
loadOk = true;
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (loadOk)
|
|||
|
|
{
|
|||
|
|
if (doc.DocumentElement.ChildNodes.Count > 0)
|
|||
|
|
{
|
|||
|
|
dt.TableName = doc.DocumentElement.Name;
|
|||
|
|
if (dt.Columns.Count == 0)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD>绯<EFBFBD><E7BBAF><EFBFBD>ܹ<EFBFBD>
|
|||
|
|
bool useChildToGetSchema = doc.DocumentElement.ChildNodes[0].ChildNodes.Count > 0;
|
|||
|
|
foreach (XmlNode item in doc.DocumentElement.ChildNodes)
|
|||
|
|
{
|
|||
|
|
if (useChildToGetSchema)
|
|||
|
|
{
|
|||
|
|
if (item.ChildNodes.Count > 0)//<2F><><EFBFBD>ӽڵ<D3BD>,<2C><><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ֶ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
foreach (XmlNode child in item.ChildNodes)
|
|||
|
|
{
|
|||
|
|
if (!dt.Columns.Contains(child.Name))
|
|||
|
|
{
|
|||
|
|
dt.Columns.Add(child.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else//<2F><><EFBFBD><EFBFBD><EFBFBD>ӽڵ㣬<DAB5>õ<EFBFBD>ǰ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5>ֶ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (item.Attributes != null && item.Attributes.Count > 0)//<2F><><EFBFBD>ӽڵ<D3BD>,<2C><><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ֶ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
foreach (XmlAttribute attr in item.Attributes)
|
|||
|
|
{
|
|||
|
|
if (!dt.Columns.Contains(attr.Name))
|
|||
|
|
{
|
|||
|
|
dt.Columns.Add(attr.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
MDataRow dr = null;
|
|||
|
|
foreach (XmlNode row in doc.DocumentElement.ChildNodes)
|
|||
|
|
{
|
|||
|
|
dr = dt.NewRow();
|
|||
|
|
if (row.ChildNodes.Count > 0)//<2F><><EFBFBD>ӽڵ㴦<DAB5><E3B4A6>
|
|||
|
|
{
|
|||
|
|
foreach (XmlNode cell in row.ChildNodes)
|
|||
|
|
{
|
|||
|
|
if (!cell.InnerXml.StartsWith("<![CDATA["))
|
|||
|
|
{
|
|||
|
|
dr.Set(cell.Name, cell.InnerXml.Trim(), 1);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
dr.Set(cell.Name, cell.InnerText.Trim(), 1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
dt.Rows.Add(dr);
|
|||
|
|
}
|
|||
|
|
else if (row.Attributes != null && row.Attributes.Count > 0) //<2F><><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>
|
|||
|
|
{
|
|||
|
|
foreach (XmlAttribute cell in row.Attributes)
|
|||
|
|
{
|
|||
|
|
dr.Set(cell.Name, cell.Value.Trim(), 1);
|
|||
|
|
}
|
|||
|
|
dt.Rows.Add(dr);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD>е<EFBFBD>ȡֵ<EFBFBD><EFBFBD>Min<EFBFBD><EFBFBD>Max<EFBFBD><EFBFBD>Sum<EFBFBD><EFBFBD>Avg
|
|||
|
|
private T GetMinMaxValue<T>(int index, bool isMin)
|
|||
|
|
{
|
|||
|
|
if (Columns != null && index < Columns.Count && Rows != null && Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
List<T> itemList = GetColumnItems<T>(index, BreakOp.NullOrEmpty);
|
|||
|
|
if (itemList.Count > 0)
|
|||
|
|
{
|
|||
|
|
itemList.Sort();
|
|||
|
|
return isMin ? itemList[0] : itemList[itemList.Count - 1];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return default(T);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD>Сֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Min<T>(string columnName)
|
|||
|
|
{
|
|||
|
|
return GetMinMaxValue<T>(Columns.GetIndex(columnName), true);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD>Сֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="index"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Min<T>(int index)
|
|||
|
|
{
|
|||
|
|
return GetMinMaxValue<T>(index, true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Max<T>(string columnName)
|
|||
|
|
{
|
|||
|
|
return GetMinMaxValue<T>(Columns.GetIndex(columnName), false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="index"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Max<T>(int index)
|
|||
|
|
{
|
|||
|
|
return GetMinMaxValue<T>(index, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD>ֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Sum<T>(string columnName)
|
|||
|
|
{
|
|||
|
|
return Sum<T>(Columns.GetIndex(columnName));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD>ֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="index"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Sum<T>(int index)
|
|||
|
|
{
|
|||
|
|
if (Columns != null && index < Columns.Count && Rows != null && Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
List<Decimal> itemList = GetColumnItems<Decimal>(index, BreakOp.NullOrEmpty);
|
|||
|
|
if (itemList.Count > 0)
|
|||
|
|
{
|
|||
|
|
Decimal sum = 0;
|
|||
|
|
for (int i = 0; i < itemList.Count; i++)
|
|||
|
|
{
|
|||
|
|
sum += itemList[i];
|
|||
|
|
}
|
|||
|
|
return (T)StaticTool.ChangeType(sum, typeof(T));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return default(T);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD>ƽ<EFBFBD><C6BD>ֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Avg<T>(string columnName)
|
|||
|
|
{
|
|||
|
|
return Avg<T>(Columns.GetIndex(columnName));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
///<2F><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD>ƽ<EFBFBD><C6BD>ֵ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="index"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public T Avg<T>(int index)
|
|||
|
|
{
|
|||
|
|
Decimal sum = Sum<Decimal>(index);
|
|||
|
|
if (sum > 0)
|
|||
|
|
{
|
|||
|
|
return (T)StaticTool.ChangeType(sum / Rows.Count, typeof(T));
|
|||
|
|
}
|
|||
|
|
return default(T);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ת<EFBFBD><D7AA><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ָ<EFBFBD><D6B8>ʱ<EFBFBD><CAB1>Ĭ<EFBFBD><C4AC>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MDataTable Pivot()
|
|||
|
|
{
|
|||
|
|
if (Columns.Count < 3)
|
|||
|
|
{
|
|||
|
|
Error.Throw("At least three columns when call Pivot()");
|
|||
|
|
}
|
|||
|
|
int count = Columns.Count;
|
|||
|
|
return Pivot(Columns[count - 3].ColumnName, Columns[count - 2].ColumnName, Columns[count - 1].ColumnName);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="rowName"><3E><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="colName"><3E><><EFBFBD>ڷֲ<DAB7><D6B2><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="valueName"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾֵ<CABE><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataTable Pivot(string rowName, string colName, string valueName)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = new MDataTable(TableName);
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|||
|
|
List<string> colNameItems = GetColumnItems<string>(colName, BreakOp.NullOrEmpty, true);
|
|||
|
|
if (colNameItems == null || colNameItems.Count == 0 || colNameItems.Count > 255)
|
|||
|
|
{
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
dt.Columns.Add(rowName);
|
|||
|
|
for (int i = 0; i < colNameItems.Count; i++)
|
|||
|
|
{
|
|||
|
|
dt.Columns.Add(colNameItems[i]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
List<string> rowNameItems = GetColumnItems<string>(rowName, BreakOp.None, true);
|
|||
|
|
MDataTable splitTable = this;
|
|||
|
|
for (int i = 0; i < rowNameItems.Count; i++)
|
|||
|
|
{
|
|||
|
|
MDataRow nameRow = dt.NewRow(true).Set(0, rowNameItems[i]);//<2F>±<EFBFBD><C2B1><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
|
MDataTable[] dt2 = splitTable.Split(rowName + "='" + rowNameItems[i] + "'");//ɸѡ<C9B8>ָ<EFBFBD>
|
|||
|
|
splitTable = dt2[1];//ʣ<>µ<EFBFBD><C2B5><EFBFBD>Ϊ<EFBFBD>´ηָ<CEB7>
|
|||
|
|
|
|||
|
|
foreach (MDataRow row in dt2[0].Rows)//<2F><>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (!row[colName].IsNullOrEmpty)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ջ<EFBFBD>Null
|
|||
|
|
{
|
|||
|
|
nameRow.Set(row[colName].Value, row[valueName].Value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
//<2F><>MDataTableBatchAction<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>á<EFBFBD>
|
|||
|
|
internal void Load(MDataTable dt, MCellStruct primary)
|
|||
|
|
{
|
|||
|
|
if (dt == null || dt.Rows.Count == 0)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
string pkName = primary != null ? primary.ColumnName : Columns.FirstPrimary.ColumnName;
|
|||
|
|
int i1 = Columns.GetIndex(pkName);
|
|||
|
|
MDataRow rowA, rowB;
|
|||
|
|
|
|||
|
|
for (int i = 0; i < Rows.Count; i++)
|
|||
|
|
{
|
|||
|
|
rowA = Rows[i];
|
|||
|
|
rowB = dt.FindRow(pkName + "='" + rowA[i1].StringValue + "'");
|
|||
|
|
if (rowB != null)
|
|||
|
|
{
|
|||
|
|
rowA.LoadFrom(rowB, RowOp.IgnoreNull, false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region ע<EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/*
|
|||
|
|
*
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>List<73>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD>MDataTable
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="entityList">ʵ<><CAB5><EFBFBD>б<EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static MDataTable LoadFromList<T>(List<T> entityList) where T : class
|
|||
|
|
{
|
|||
|
|
MDataTable dt = new MDataTable("Default");
|
|||
|
|
if (entityList != null && entityList.Count > 0)
|
|||
|
|
{
|
|||
|
|
dt.Columns = SchemaCreate.GetColumns(entityList[0].GetType());
|
|||
|
|
//<2F><><EFBFBD>ɱ<EFBFBD><C9B1>ṹ<EFBFBD><E1B9B9>
|
|||
|
|
foreach (T entity in entityList)
|
|||
|
|
{
|
|||
|
|
MDataRow row = dt.NewRow();
|
|||
|
|
row.LoadFrom(entity);
|
|||
|
|
dt.Rows.Add(row);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ٴ<EFBFBD><D9B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD>ID<49><44><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ϵͳ<CFB5>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ID<49>е<EFBFBD><D0B5><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"><3E>ļ<EFBFBD><C4BC><EFBFBD></param>
|
|||
|
|
/// <param name="overwrite">ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="columnNames"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="sqlDbTypes"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊnvarchar<61><72></param>
|
|||
|
|
public static void CreateSchema(string fileName, bool overwrite, string[] columnNames, params SqlDbType[] sqlDbTypes)
|
|||
|
|
{
|
|||
|
|
if (columnNames.Length >= 0)
|
|||
|
|
{
|
|||
|
|
if (fileName[1] != ':')
|
|||
|
|
{
|
|||
|
|
fileName = AppConfig.WebRootPath + fileName;
|
|||
|
|
}
|
|||
|
|
fileName = fileName.Replace(Path.GetExtension(fileName), string.Empty) + ".ts";
|
|||
|
|
if (!File.Exists(fileName) || overwrite)
|
|||
|
|
{
|
|||
|
|
MDataColumn mdc = new MDataColumn();
|
|||
|
|
string columnName = string.Empty;
|
|||
|
|
for (int i = 0; i < columnNames.Length; i++)
|
|||
|
|
{
|
|||
|
|
columnName = columnNames[i];
|
|||
|
|
if (sqlDbTypes != null && sqlDbTypes.Length > i)
|
|||
|
|
{
|
|||
|
|
mdc.Add(columnName, sqlDbTypes[i]);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
mdc.Add(columnName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (mdc[0].ColumnName.ToLower() != "id")
|
|||
|
|
{
|
|||
|
|
MCellStruct cellStruct = new MCellStruct("ID", SqlDbType.Int, true, false, -1);
|
|||
|
|
cellStruct.IsPrimaryKey = true;
|
|||
|
|
mdc.Insert(0, cellStruct);
|
|||
|
|
}
|
|||
|
|
else if (mdc[0].SqlType == SqlDbType.Int)
|
|||
|
|
{
|
|||
|
|
mdc[0].IsAutoIncrement = true;
|
|||
|
|
mdc[0].IsPrimaryKey = true;
|
|||
|
|
}
|
|||
|
|
mdc.WriteSchema(fileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
* */
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|