tijian_tieying/web/cyqdata-master/Table/MDataRow.cs

1475 lines
47 KiB
C#
Raw Normal View History

2025-02-20 12:14:39 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.ComponentModel;
using CYQ.Data.SQL;
using CYQ.Data.Tool;
using CYQ.Data.Extension;
using System.Reflection;
using System.Collections.Specialized;
using CYQ.Data.UI;
namespace CYQ.Data.Table
{
/// <summary>
/// һ<>м<EFBFBD>¼
/// </summary>
public partial class MDataRow : IDataRecord
{
List<MDataCell> _CellList;
List<MDataCell> CellList
{
get
{
if (_CellList.Count == 0 && _Table != null && _Table.Columns.Count > 0)
{
MCellStruct cellStruct;
foreach (MCellStruct item in _Table.Columns)
{
cellStruct = item;
MDataCell cell = new MDataCell(ref cellStruct, null);
_CellList.Add(cell);
}
}
return _CellList;
}
set
{
_CellList = value;
}
}
public MDataRow()
{
CellList = new List<MDataCell>();
}
public MDataRow(MDataTable dt)
{
if (dt != null)
{
_Table = dt;
CellList = new List<MDataCell>(dt.Columns.Count);
}
}
public MDataRow(MDataColumn mdc)
{
CellList = new List<MDataCell>(mdc.Count);
Table.Columns.AddRange(mdc);
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1>ͷ
/// </summary>
public MDataColumn Columns
{
get
{
return Table.Columns;
}
}
public static implicit operator MDataRow(DataRow row)
{
if (row == null)
{
return null;
}
MDataRow mdr = new MDataRow();
mdr.TableName = row.Table.TableName;
DataColumnCollection columns = row.Table.Columns;
if (columns != null && columns.Count > 0)
{
for (int i = 0; i < columns.Count; i++)
{
MCellStruct cellStruct = new MCellStruct(columns[i].ColumnName, DataType.GetSqlType(columns[i].DataType), columns[i].ReadOnly, columns[i].AllowDBNull, columns[i].MaxLength);
cellStruct.DefaultValue = columns[i].DefaultValue;
mdr.Add(new MDataCell(ref cellStruct, row[i]));
}
}
return mdr;
}
private string _Conn = string.Empty;
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[<5B><>MAction<6F>Ӵ<EFBFBD><D3B4>мܹ<D0BC><DCB9><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>ʱ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ȳ<EFBFBD>ΪĬ<CEAA>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>]
/// </summary>
public string Conn
{
get
{
if (_Table != null)
{
return _Table.Conn;
}
else if (string.IsNullOrEmpty(_Conn))
{
return AppConfig.DB.DefaultConn;
}
return _Conn;
}
set
{
if (_Table != null)
{
_Table.Conn = value;
}
else
{
_Conn = value;
}
}
}
private string _TableName;
/// <summary>
/// ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>]
/// </summary>
public string TableName
{
get
{
if (_Table != null)
{
return _Table.TableName;
}
return _TableName;
}
set
{
if (_Table != null)
{
_Table.TableName = value;
}
else
{
_TableName = value;
}
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public MDataCell this[object field]
{
get
{
if (field is int || (field is Enum && AppConfig.IsEnumToInt))
{
int index = (int)field;
if (Count > index)
{
return this[index];
}
}
else if (field is string)
{
return this[field as string];
}
else if (field is IField)
{
IField iFiled = field as IField;
if (iFiled.ColID > -1)
{
return this[iFiled.ColID];
}
return this[iFiled.Name];
}
return this[field.ToString()];
}
}
public MDataCell this[string key]
{
get
{
int index = -1;
if (key.Length <= Count.ToString().Length) //2<=20
{
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>֡<EFBFBD>
if (!int.TryParse(key, out index))
{
index = -1;
}
}
if (index == -1)
{
index = Columns.GetIndex(key);//<2F><><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>һ<EFBFBD>¡<EFBFBD>
}
if (index > -1)
{
return this[index];
}
return null;
}
}
private MDataTable _Table;
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD> MDataTable<6C><65>
/// </summary>
public MDataTable Table
{
get
{
if (_Table == null)
{
_Table = new MDataTable(_TableName);
if (this.Count > 0)
{
foreach (MDataCell cell in this)
{
_Table.Columns.Add(cell.Struct);
}
}
_Table.Rows.Add(this);
}
return _Table;
}
internal set
{
_Table = value;
}
}
/// <summary>
/// ͨ<><CDA8>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
/// </summary>
public object[] ItemArray
{
get
{
object[] values = new object[Count];
for (int i = 0; i < values.Length; i++)
{
values[i] = this[i].Value;
}
return values;
}
}
private string _RowError;
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD>
/// </summary>
public string RowError
{
get { return _RowError; }
set { _RowError = value; }
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public MDataCell PrimaryCell
{
get
{
return JointPrimaryCell[0];
}
}
private List<MDataCell> _JointPrimaryCell;
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public List<MDataCell> JointPrimaryCell
{
get
{
if (_JointPrimaryCell == null && Columns.Count > 0)
{
_JointPrimaryCell = new List<MDataCell>(Columns.JointPrimary.Count);
foreach (MCellStruct st in Columns.JointPrimary)
{
_JointPrimaryCell.Add(this[st.ColumnName]);
}
}
return _JointPrimaryCell;
}
}
/// <summary>
/// <20>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD>Emit<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public object GetItemValue(int index)//<2F><><EFBFBD><EFBFBD>Public
{
MDataCell cell = this[index];
if (cell == null || cell.Value == null || cell.Value == DBNull.Value)
{
return null;
}
return cell.Value;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
/// </summary>
/// <returns></returns>
public object[] GetItemValues()
{
object[] values = new object[Columns.Count];
for (int i = 0; i < this.Count; i++)
{
values[i] = this[i].Value;
}
return values;
}
/// <summary>
/// ȡֵ
/// </summary>
public T Get<T>(object key)
{
return Get<T>(key, default(T));
}
public T Get<T>(object key, T defaultValue)
{
MDataCell cell = this[key];
if (cell == null || cell.IsNull)
{
return defaultValue;
}
return cell.Get<T>();
}
/// <summary>
/// <20><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ColumnName<6D><65>Value<75><65><EFBFBD>ı<EFBFBD>
/// </summary>
public MDataTable ToTable()
{
MDataTable dt = this.Columns.ToTable();
MCellStruct msValue = new MCellStruct("Value", SqlDbType.NVarChar);
MCellStruct msState = new MCellStruct("State", SqlDbType.Int);
dt.Columns.Insert(1, msValue);
dt.Columns.Insert(2, msState);
for (int i = 0; i < Count; i++)
{
dt.Rows[i][1].Value = this[i].Value;
dt.Rows[i][2].Value = this[i].State;
}
return dt;
}
/// <summary>
/// <20><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ֵȫ<D6B5><C8AB><EFBFBD><EFBFBD>ΪNull
/// </summary>
public void Clear()
{
for (int i = 0; i < this.Count; i++)
{
this[i].Clear();
}
}
/// <summary>
/// <20><>ȡ<EFBFBD>еĵ<D0B5>ǰ״̬[0:δ<><CEB4><EFBFBD>ģ<EFBFBD>1:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɲ<EFBFBD><C9B2><EFBFBD>]<5D><>2:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɸ<EFBFBD><C9B8><EFBFBD>]]
/// </summary>
/// <returns></returns>
public int GetState()
{
return GetState(false);
}
/// <summary>
/// <20><>ȡ<EFBFBD>еĵ<D0B5>ǰ״̬[0:δ<><CEB4><EFBFBD>ģ<EFBFBD>1:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɲ<EFBFBD><C9B2><EFBFBD>]<5D><>2:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɸ<EFBFBD><C9B8><EFBFBD>]]
/// </summary>
public int GetState(bool ignorePrimaryKey)
{
int state = 0;
for (int i = 0; i < this.Count; i++)
{
MDataCell cell = this[i];
if (ignorePrimaryKey && cell.Struct.IsPrimaryKey)
{
continue;
}
state = cell.State > state ? cell.State : state;
}
return state;
}
/// <summary>
/// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
/// </summary>
public MDataRow Set(object key, object value)
{
return Set(key, value, -1);
}
/// <summary>
/// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
/// </summary>
/// <param name="key"><3E>ֶ<EFBFBD><D6B6><EFBFBD></param>
/// <param name="value">ֵ</param>
/// <param name="state"><3E>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>״̬[0:δ<><CEB4><EFBFBD>ģ<EFBFBD>1:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɲ<EFBFBD><C9B2><EFBFBD>]<5D><>2:<3A>Ѹ<EFBFBD>ֵ,ֵ<><D6B5>ͬ[<5B>ɸ<EFBFBD><C9B8><EFBFBD>]]</param>
public MDataRow Set(object key, object value, int state)
{
MDataCell cell = this[key];
if (cell != null)
{
cell.Value = value;
if (state > -1 && state < 3)
{
cell.State = state;
}
}
return this;
}
/// <summary>
/// <20><><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 MDataRow SetState(int state)
{
return SetState(state, BreakOp.None);
}
/// <summary>
/// <20><><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 MDataRow SetState(int state, BreakOp op)
{
return SetState(state, op, string.Empty);
}
/// <param name="columns"><para><3E><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ijЩ<C4B3><D0A9></para></param>
/// <returns></returns>
public MDataRow SetState(int state, BreakOp op, string columns)
{
if (!string.IsNullOrEmpty(columns))
{
string[] items = columns.Trim(',', ' ').Split(',');
for (int i = 0; i < items.Length; i++)
{
MDataCell cell = this[items[i]];
if (cell != null)
{
SetState(state, op, cell);
}
}
}
else
{
for (int i = 0; i < this.Count; i++)
{
SetState(state, op, this[i]);
}
}
return this;
}
private void SetState(int state, BreakOp op, MDataCell cell)
{
switch (op)
{
case BreakOp.Null:
if (cell.IsNull)
{
return;
}
break;
case BreakOp.Empty:
if (cell.StringValue == "")
{
return;
}
break;
case BreakOp.NullOrEmpty:
if (cell.IsNullOrEmpty)
{
return;
}
break;
}
cell.State = state;
}
#region ICloneable <EFBFBD><EFBFBD>Ա
/// <summary>
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
/// </summary>
/// <returns></returns>
public MDataRow Clone()
{
MDataRow row = new MDataRow();
for (int i = 0; i < Count; i++)
{
MCellStruct mcb = this[i].Struct;
MDataCell mdc = new MDataCell(ref mcb);
mdc.LoadValue(this[i]);
row.Add(mdc);
}
//row._Table = _Table;//<2F><><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD>õ<EFBFBD><C3B5>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
row.RowError = RowError;
row.TableName = TableName;
row.Conn = Conn;
return row;
}
#endregion
#region IDataRecord <EFBFBD><EFBFBD>Ա
int IDataRecord.FieldCount
{
get
{
return Count;
}
}
bool IDataRecord.GetBoolean(int i)
{
return (bool)this[i].Value;
}
byte IDataRecord.GetByte(int i)
{
return (byte)this[i].Value;
}
long IDataRecord.GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
return (byte)this[i].Value;
}
char IDataRecord.GetChar(int i)
{
return (char)this[i].Value;
}
long IDataRecord.GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
return (long)this[i].Value;
}
IDataReader IDataRecord.GetData(int i)
{
return null;
}
string IDataRecord.GetDataTypeName(int i)
{
//return "";
return this[i].Struct.SqlTypeName;
}
DateTime IDataRecord.GetDateTime(int i)
{
return (DateTime)this[i].Value;
}
decimal IDataRecord.GetDecimal(int i)
{
return (decimal)this[i].Value;
}
double IDataRecord.GetDouble(int i)
{
return (double)this[i].Value;
}
Type IDataRecord.GetFieldType(int i)
{
return this[i].Struct.ValueType;
}
float IDataRecord.GetFloat(int i)
{
return (float)this[i].Value;
}
Guid IDataRecord.GetGuid(int i)
{
return (Guid)this[i].Value;
}
short IDataRecord.GetInt16(int i)
{
return (short)this[i].Value;
}
int IDataRecord.GetInt32(int i)
{
return (int)this[i].Value;
}
long IDataRecord.GetInt64(int i)
{
return (long)this[i].Value;
}
string IDataRecord.GetName(int i)
{
return this[i].ColumnName;
}
int IDataRecord.GetOrdinal(string name)
{
return this.Columns.GetIndex(name);
}
string IDataRecord.GetString(int i)
{
return (string)this[i].Value;
}
object IDataRecord.GetValue(int i)
{
return this[i].Value;
}
int IDataRecord.GetValues(object[] values)
{
if (values != null && this.Count == values.Length)
{
for (int i = 0; i < this.Count; i++)
{
values[i] = this[i].Value;
}
}
return this.Count;
}
bool IDataRecord.IsDBNull(int i)
{
return this[i].IsNull;
}
object IDataRecord.this[string name]
{
get
{
return this[name];
}
}
object IDataRecord.this[int i]
{
get
{
return this[i];
}
}
#endregion
}
public partial class MDataRow : IList<MDataCell>
{
public int Count
{
get { return CellList.Count; }
}
public MDataCell this[int index]
{
get
{
return CellList[index];
}
set
{
Error.Throw(AppConst.Global_NotImplemented);
}
}
public void Add(string columnName, object value)
{
Add(columnName, SqlDbType.NVarChar, value);
}
public void Add(string columnName, SqlDbType sqlType, object value)
{
MCellStruct cs = new MCellStruct(columnName, sqlType, false, true, -1);
Add(new MDataCell(ref cs, value));
}
public void Add(MDataCell cell)
{
CellList.Add(cell);
Columns.Add(cell.Struct);
}
public void Insert(int index, MDataCell cell)
{
CellList.Insert(index, cell);
Columns.Insert(index, cell.Struct);
}
public void Remove(string columnName)
{
int index = Columns.GetIndex(columnName);
if (index > -1)
{
RemoveAt(index);
}
}
public bool Remove(MDataCell item)
{
if (Columns.Count == Count)
{
Columns.Remove(item.Struct);
}
else
{
CellList.Remove(item);
}
return true;
}
public void RemoveAt(int index)
{
if (Columns.Count == Count)
{
Columns.RemoveAt(index);
}
else
{
CellList.RemoveAt(index);
}
}
#region IList<MDataCell> <EFBFBD><EFBFBD>Ա
int IList<MDataCell>.IndexOf(MDataCell item)
{
return CellList.IndexOf(item);
}
#endregion
#region ICollection<MDataCell> <EFBFBD><EFBFBD>Ա
public bool Contains(MDataCell item)
{
return CellList.Contains(item);
}
void ICollection<MDataCell>.CopyTo(MDataCell[] array, int arrayIndex)
{
CellList.CopyTo(array, arrayIndex);
}
bool ICollection<MDataCell>.IsReadOnly
{
get { return false; }
}
#endregion
#region IEnumerable<MDataCell> <EFBFBD><EFBFBD>Ա
IEnumerator<MDataCell> IEnumerable<MDataCell>.GetEnumerator()
{
return CellList.GetEnumerator();
}
#endregion
#region IEnumerable <EFBFBD><EFBFBD>Ա
IEnumerator IEnumerable.GetEnumerator()
{
return CellList.GetEnumerator();
}
#endregion
}
//<2F><>չ<EFBFBD><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public partial class MDataRow
{
/// <summary>
/// <20><>ʵ<EFBFBD>塢Json<6F><6E>Xml<6D><6C>IEnumerable<6C>ӿ<EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD><D6B5>ࡢMDataRow
/// </summary>
/// <returns></returns>
public static MDataRow CreateFrom(object anyObj)
{
return CreateFrom(anyObj, null);
}
public static MDataRow CreateFrom(object anyObj, Type valueType)
{
return CreateFrom(anyObj, valueType, BreakOp.None);
}
public static MDataRow CreateFrom(object anyObj, Type valueType, BreakOp op)
{
return CreateFrom(anyObj, valueType, op, JsonHelper.DefaultEscape);
}
/// <summary>
/// <20><>ʵ<EFBFBD>塢Json<6F><6E>Xml<6D><6C>IEnumerable<6C>ӿ<EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD><D6B5>ࡢMDataRow
/// </summary>
public static MDataRow CreateFrom(object anyObj, Type valueType, BreakOp breakOp, EscapeOp escapeOp)
{
MDataRow row = new MDataRow();
if (anyObj is string)
{
row.LoadFrom(anyObj as string, escapeOp, breakOp);
}
else if (anyObj is IEnumerable)
{
row.LoadFrom(anyObj as IEnumerable, valueType, escapeOp, breakOp);
}
else if (anyObj is MDataRow)
{
row.LoadFrom(row as MDataRow, (breakOp == BreakOp.Null ? RowOp.IgnoreNull : RowOp.None), true);
}
else
{
row.LoadFrom(anyObj, breakOp);
}
row.SetState(1);//<2F>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬Ĭ<CCAC><C4AC><EFBFBD><EFBFBD>Ϊ1.
return row;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>Json
/// </summary>
public string ToJson()
{
return ToJson(RowOp.IgnoreNull, false);
}
public string ToJson(bool isConvertNameToLower)
{
return ToJson(RowOp.IgnoreNull, isConvertNameToLower);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>Json
/// </summary>
public string ToJson(RowOp op)
{
return ToJson(op, false);
}
public string ToJson(RowOp op, bool isConvertNameToLower)
{
return ToJson(op, isConvertNameToLower, EscapeOp.Default);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>Json
/// </summary>
/// <param name="op"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="escapeOp">ת<><D7AA>ѡ<EFBFBD><D1A1></param>
/// <returns></returns>
public string ToJson(RowOp op, bool isConvertNameToLower, EscapeOp escapeOp)
{
JsonHelper helper = new JsonHelper();
helper.IsConvertNameToLower = isConvertNameToLower;
helper.Escape = escapeOp;
helper.RowOp = op;
helper.Fill(this);
return helper.ToString();
}
internal string ToXml(bool isConvertNameToLower)
{
string xml = string.Empty;
foreach (MDataCell cell in this)
{
xml += cell.ToXml(isConvertNameToLower);
}
return xml;
}
/*
//public bool WriteJson(string fileName)
//{
// return WriteJson(fileName, RowOp.IgnoreNull);
//}
/// <summary>
/// <20><>json<6F><6E><EFBFBD>浽ָ<E6B5BD><D6B8><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
/// </summary>
public bool WriteJson(string fileName, RowOp op)
{
return IOHelper.Write(fileName, ToJson(op));
}
*/
/// <summary>
/// ת<><D7AA>ʵ<EFBFBD><CAB5>
/// </summary>
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
public T ToEntity<T>()
{
//Type t = typeof(T);
//switch (StaticTool.GetSystemType(ref t))
//{
// case SysType.Base:
// return (T)StaticTool.ChangeType(this[0].Value, t);
//}
//object obj = Activator.CreateInstance(t);
//SetToEntity(ref obj, this);
//return (T)obj;
return (T)ToEntity(typeof(T));
}
internal object ToEntity(Type t)
{
switch (StaticTool.GetSystemType(ref t))
{
case SysType.Base:
return StaticTool.ChangeType(this[0].Value, t);
}
object obj = Activator.CreateInstance(t);
SetToEntity(ref obj, this);
return obj;
}
private object GetValue(MDataRow row, Type type)
{
switch (StaticTool.GetSystemType(ref type))
{
case SysType.Base:
return StaticTool.ChangeType(row[0].Value, type);
case SysType.Enum:
return Enum.Parse(type, row[0].ToString());
default:
object o = Activator.CreateInstance(type);
SetToEntity(ref o, row);
return o;
}
}
/// <summary>
/// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UI
/// </summary>
public void SetToAll(params object[] parentControls)
{
SetToAll(null, parentControls);
}
/// <summary>
/// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UI
/// </summary>
/// <param name="autoPrefix"><3E>Զ<EFBFBD>ǰ׺<C7B0><D7BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6>ŷָ<C5B7></param>
/// <param name="parentControls">ҳ<><D2B3><EFBFBD>ؼ<EFBFBD></param>
public void SetToAll(string autoPrefix, params object[] parentControls)
{
if (Count > 0)
{
MDataRow row = this;
using (MActionUI mui = new MActionUI(ref row, null, null))
{
if (!string.IsNullOrEmpty(autoPrefix))
{
string[] pres = autoPrefix.Split(',');
mui.SetAutoPrefix(pres[0], pres);
}
mui.SetAll(parentControls);
}
}
}
/// <summary>
/// <20><>Web Post<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡֵ<C8A1><D6B5>
/// </summary>
public void LoadFrom()
{
LoadFrom(true);
}
/// <summary>
/// <20><>Web Post<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡֵ <20><> <20><>Winform<72><6D>WPF<50>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD>ȡֵ<C8A1><D6B5>
/// <param name="isWeb">TrueΪWebӦ<62>ã<EFBFBD><C3A3><EFBFBD>֮ΪWinӦ<6E><D3A6></param>
/// <param name="prefixOrParentControl">Webʱ<62><CAB1><EFBFBD><EFBFBD>ǰ׺<C7B0><D7BA>Winʱ<6E><CAB1><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD></param>
/// </summary>
public void LoadFrom(bool isWeb, params object[] prefixOrParentControl)
{
if (Count > 0)
{
MDataRow row = this;
using (MActionUI mui = new MActionUI(ref row, null, null))
{
if (prefixOrParentControl.Length > 0)
{
if (isWeb)
{
string[] items = prefixOrParentControl as string[];
mui.SetAutoPrefix(items[0], items);
}
else
{
mui.SetAutoParentControl(prefixOrParentControl[0], prefixOrParentControl);
}
}
mui.GetAll(false);
}
}
}
/// <summary>
/// <20>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>ֵ
/// </summary>
public void LoadFrom(MDataRow row)
{
LoadFrom(row, RowOp.None, Count == 0);
}
/// <summary>
/// <20>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>ֵ
/// </summary>
public void LoadFrom(MDataRow row, RowOp rowOp, bool isAllowAppendColumn)
{
LoadFrom(row, rowOp, isAllowAppendColumn, true);
}
/// <summary>
/// <20>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>ֵ
/// </summary>
/// <param name="row">Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD></param>
/// <param name="rowOp"><3E><>ѡ<EFBFBD><D1A1>[<5B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>]</param>
/// <param name="isAllowAppendColumn"><3E><><EFBFBD><EFBFBD>row<6F><77><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="isWithValueState"><3E>Ƿ<EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>״̬[Ĭ<><C4AC>ֵΪ<D6B5><CEAA>true]</param>
public void LoadFrom(MDataRow row, RowOp rowOp, bool isAllowAppendColumn, bool isWithValueState)
{
if (row != null)
{
if (isAllowAppendColumn)
{
for (int i = 0; i < row.Count; i++)
{
//if (rowOp == RowOp.IgnoreNull && row[i].IsNull)
//{
// continue;
//}
if (!Columns.Contains(row[i].ColumnName))
{
Columns.Add(row[i].Struct);
}
}
}
MDataCell rowCell;
foreach (MDataCell cell in this)
{
rowCell = row[cell.ColumnName];
if (rowCell == null || (rowOp == RowOp.IgnoreNull && rowCell.IsNull))
{
continue;
}
if (rowOp == RowOp.None || rowCell.State >= (int)rowOp)
{
cell.Value = rowCell.Value;//<2F><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ͬ<EFBFBD>ļܹ<C4BC><DCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> int[access] int64[sqlite]<5D><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//cell._CellValue.IsNull = rowCell._CellValue.IsNull;//
if (isWithValueState)
{
cell.State = rowCell.State;
}
}
}
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
/// </summary>
/// <param name="values"></param>
public void LoadFrom(object[] values)
{
if (values != null && values.Length <= Count)
{
for (int i = 0; i < values.Length; i++)
{
this[i].Value = values[i];
}
}
}
public void LoadFrom(string json)
{
LoadFrom(json, JsonHelper.DefaultEscape);
}
/// <summary>
/// <20><>json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
/// </summary>
public void LoadFrom(string json, EscapeOp op)
{
LoadFrom(json, JsonHelper.DefaultEscape, BreakOp.None);
}
/// <summary>
/// <20><>json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
/// </summary>
public void LoadFrom(string json, EscapeOp op, BreakOp breakOp)
{
if (!string.IsNullOrEmpty(json))
{
Dictionary<string, string> dic = JsonHelper.Split(json);
if (dic != null && dic.Count > 0)
{
LoadFrom(dic, null, op, breakOp);
}
}
else
{
LoadFrom(true);
}
}
/// <summary>
/// <20>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD>ֵ伯<D6B5><E4BCAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public void LoadFrom(IEnumerable dic)
{
LoadFrom(dic, null, JsonHelper.DefaultEscape, BreakOp.None);
}
internal void LoadFrom(IEnumerable dic, Type valueType, EscapeOp op, BreakOp breakOp)
{
if (dic != null)
{
bool isNameValue = dic is NameValueCollection;
bool isAddColumn = Columns.Count == 0;
SqlDbType sdt = SqlDbType.NVarChar;
if (isAddColumn)
{
if (valueType != null)
{
sdt = DataType.GetSqlType(valueType);
}
else if (!isNameValue)
{
Type type = dic.GetType();
if (type.IsGenericType)
{
sdt = DataType.GetSqlType(type.GetGenericArguments()[1]);
}
else
{
sdt = SqlDbType.Variant;
}
}
}
string key = null; object value = null;
Type t = null;
int i = -1;
foreach (object o in dic)
{
i++;
if (isNameValue)
{
if (o == null)
{
key = "null";
value = ((NameValueCollection)dic)[i];
}
else
{
key = Convert.ToString(o);
value = ((NameValueCollection)dic)[key];
}
}
else
{
t = o.GetType();
value = t.GetProperty("Value").GetValue(o, null);
bool isContinue = false;
switch (breakOp)
{
case BreakOp.Null:
if (value == null)
{
isContinue = true;
}
break;
case BreakOp.Empty:
if (value != null && Convert.ToString(value) == "")
{
isContinue = true;
}
break;
case BreakOp.NullOrEmpty:
if (Convert.ToString(value) == "")
{
isContinue = true;
}
break;
}
if (isContinue) { continue; }
key = Convert.ToString(t.GetProperty("Key").GetValue(o, null));
//if (value != null)
//{
//}
}
//if (value != null)
//{
if (isAddColumn)
{
SqlDbType sdType = sdt;
if (sdt == SqlDbType.Variant)
{
if (value == null)
{
sdType = SqlDbType.NVarChar;
}
else
{
sdType = DataType.GetSqlType(value.GetType());
}
}
Add(key, sdType, value);
}
else
{
if (value != null && value is string)
{
value = JsonHelper.UnEscape(value.ToString(), op);
}
Set(key, value);
}
// }
}
}
}
/// <summary>
/// <20><>ʵ<EFBFBD><CAB5>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
/// </summary>
/// <param name="entity">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
public void LoadFrom(object entity)
{
if (entity == null || entity is Boolean)
{
LoadFrom(true);
}
else if (entity is String)
{
LoadFrom(entity as String);
}
else if (entity is MDataRow)
{
LoadFrom(entity as MDataRow);
}
else if (entity is IEnumerable)
{
LoadFrom(entity as IEnumerable);
}
else
{
LoadFrom(entity, BreakOp.None);
}
}
/// <summary>
/// <20><>ʵ<EFBFBD><CAB5>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
/// </summary>
/// <param name="entity">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
public void LoadFrom(object entity, BreakOp op)
{
if (entity == null)
{
return;
}
try
{
Type t = entity.GetType();
if (Columns.Count == 0)
{
MDataColumn mcs = TableSchema.GetColumns(t);
MCellStruct ms = null;
for (int i = 0; i < mcs.Count; i++)
{
ms = mcs[i];
MDataCell cell = new MDataCell(ref ms);
Add(cell);
}
}
if (string.IsNullOrEmpty(TableName))
{
TableName = t.Name;
}
List<PropertyInfo> pis = StaticTool.GetPropertyInfo(t);
if (pis != null)
{
foreach (PropertyInfo pi in pis)
{
int index = Columns.GetIndex(pi.Name);
if (index > -1)
{
object propValue = pi.GetValue(entity, null);
switch (op)
{
case BreakOp.Null:
if (propValue == null)
{
continue;
}
break;
case BreakOp.Empty:
if (Convert.ToString(propValue) == "")
{
continue;
}
break;
case BreakOp.NullOrEmpty:
if (propValue == null || Convert.ToString(propValue) == "")
{
continue;
}
break;
}
Set(index, propValue, 2);//<2F><><EFBFBD><EFBFBD>״̬Ӧ<CCAC><D3A6>ֵ<EFBFBD><D6B5><EFBFBD>ã<EFBFBD><C3A3><EFBFBD>Ϊ1<CEAA>Dz<EFBFBD><C7B2>Եġ<D4B5>
}
}
}
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="obj">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
public void SetToEntity(object obj)
{
SetToEntity(ref obj, this);
}
/// <summary>
/// <20><>ָ<EFBFBD><D6B8><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="obj"></param>
/// <param name="row"></param>
internal void SetToEntity(ref object obj, MDataRow row)
{
if (obj == null || row == null || row.Count == 0)
{
return;
}
Type objType = obj.GetType();
string objName = objType.FullName, cellName = string.Empty;
try
{
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
List<PropertyInfo> pis = StaticTool.GetPropertyInfo(objType);
foreach (PropertyInfo p in pis)//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
{
cellName = p.Name;
MDataCell cell = row[cellName];
if (cell == null || cell.IsNull)
{
continue;
}
Type propType = p.PropertyType;
object objValue = null;
SysType sysType = StaticTool.GetSystemType(ref propType);
switch (sysType)
{
case SysType.Enum:
p.SetValue(obj, Enum.Parse(propType, cell.ToString()), null);
break;
case SysType.Base:
if (propType.Name == "String")
{
//ȥ<><C8A5>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (cell.StringValue.IndexOf("\\\"") > -1)
{
p.SetValue(obj, cell.StringValue.Replace("\\\"", "\""), null);
}
else
{
p.SetValue(obj, cell.StringValue, null);
}
}
else
{
object value = StaticTool.ChangeType(cell.Value, p.PropertyType);
p.SetValue(obj, value, null);
}
break;
case SysType.Array:
case SysType.Collection:
case SysType.Generic:
if (cell.Value.GetType() == propType)
{
objValue = cell.Value;
}
else
{
Type[] argTypes = null;
int len = StaticTool.GetArgumentLength(ref propType, out argTypes);
if (len == 1) // Table
{
if (JsonSplit.IsJson(cell.StringValue) && cell.StringValue.Contains(":") && cell.StringValue.Contains("{"))
{
#region JsonǶ<EFBFBD>״<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MDataTable dt = MDataTable.CreateFrom(cell.StringValue);//, SchemaCreate.GetColumns(argTypes[0])
objValue = Activator.CreateInstance(propType, dt.Rows.Count);//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
Type objListType = objValue.GetType();
foreach (MDataRow rowItem in dt.Rows)
{
object o = GetValue(rowItem, argTypes[0]);
MethodInfo method = objListType.GetMethod("Add");
if (method == null)
{
method = objListType.GetMethod("Push");
}
if (method != null)
{
method.Invoke(objValue, new object[] { o });
}
}
dt = null;
#endregion
}
else
{
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
List<string> items = JsonSplit.SplitEscapeArray(cell.StringValue);//<2F>ڲ<EFBFBD>ȥ<EFBFBD><C8A5>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (items == null) { continue; }
objValue = Activator.CreateInstance(propType, items.Count);//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
Type objListType = objValue.GetType();
bool isArray = sysType == SysType.Array;
for (int i = 0; i < items.Count; i++)
{
MethodInfo method;
if (isArray)
{
Object item = StaticTool.ChangeType(items[i], Type.GetType(propType.FullName.Replace("[]", "")));
method = objListType.GetMethod("Set");
if (method != null)
{
method.Invoke(objValue, new object[] { i, item });
}
}
else
{
Object item = StaticTool.ChangeType(items[i], argTypes[0]);
method = objListType.GetMethod("Add");
if (method == null)
{
method = objListType.GetMethod("Push");
}
if (method != null)
{
method.Invoke(objValue, new object[] { item });
}
}
}
#endregion
}
}
else if (len == 2) // row
{
MDataRow mRow = MDataRow.CreateFrom(cell.Value, argTypes[1]);
objValue = Activator.CreateInstance(propType, mRow.Columns.Count);//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
foreach (MDataCell mCell in mRow)
{
object mObj = GetValue(mCell.ToRow(), argTypes[1]);
objValue.GetType().GetMethod("Add").Invoke(objValue, new object[] { mCell.ColumnName, mObj });
}
mRow = null;
}
}
p.SetValue(obj, objValue, null);
break;
case SysType.Custom://<2F><><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD>
MDataRow mr = new MDataRow(TableSchema.GetColumns(propType));
mr.LoadFrom(cell.ToString());
objValue = Activator.CreateInstance(propType);
SetToEntity(ref objValue, mr);
mr = null;
p.SetValue(obj, objValue, null);
break;
}
}
#endregion
}
catch (Exception err)
{
string msg = "[AttachInfo]:" + string.Format("ObjName:{0} PropertyName:{1}", objName, cellName) + "\r\n";
msg += Log.GetExceptionMessage(err);
Log.WriteLogToTxt(msg);
}
}
}
}