123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Collections;
- using System.ComponentModel;
- namespace Ant.ORM.Table
- {
- /// <summary>
- /// 一行记录
- /// </summary>
- public class MDataRow : List<MDataCell>, IDataRecord, System.ComponentModel.ICustomTypeDescriptor
- {
- public MDataRow()
- : base()
- {
- }
- private string _TableName;
- public string TableName
- {
- get
- {
- return _TableName;
- }
- set
- {
- _TableName = value;
- }
- }
- /// <summary>
- /// 输入枚举型数据
- /// </summary>
- public MDataCell this[object filed]
- {
- get
- {
- if (filed is Enum || filed is int)
- {
- return base[(int)filed];
- }
- return this[filed.ToString()];
- }
- }
- public MDataCell this[string key]
- {
- get
- {
- MDataCell dataCell = null;
- for (int i = 0; i < base.Count; i++)
- {
- if (string.Compare(base[i]._CellStruct.ColumnName, key, true) == 0)
- {
- dataCell = base[i];
- break;
- }
- }
- return dataCell;
- }
- }
- /// <summary>
- /// 此方法为Emit所调用编写。
- /// </summary>
- public object GetItemValue(int index)
- {
- MDataCell cell = this[index];
- if (cell == null || cell.Value == null || cell.Value == DBNull.Value)
- {
- return null;
- }
- return cell.Value;
- }
- /// <summary>
- /// 取值
- /// </summary>
- public T Get<T>(object key)
- {
- object value = this[key].Value;
- if (value == null || value == DBNull.Value)
- {
- return default(T);
- }
- switch (typeof(T).Name)
- {
- case "Int32":
- value = Convert.ToInt32(value);
- break;
- //case "Single":
- //case "Double":
- //case "Decimal":
- //case "DateTime":
- // break;
- //default:
- case "String":
- value = value.ToString();
- break;
- }
- return (T)value;
- }
- /// <summary>
- /// 将行的数据行的值全重置为Null
- /// </summary>
- public new void Clear()
- {
- for (int i = 0; i < this.Count; i++)
- {
- this[i]._CellValue.Value = null;
- this[i]._CellValue.IsChange = false;
- this[i]._CellValue.IsNull = true;
- }
- }
- public void Set(object key, object value)
- {
- MDataCell cell = this[key];
- if (cell != null)
- {
- cell.Value = value;
- }
- }
- public MDataTable ToTable()
- {
- MDataTable mTable = new MDataTable(_TableName);
- mTable.LoadRow(this);
- return mTable;
- }
- /// <summary>
- /// 从别的行加载值[不改变自身结构及引用]
- /// </summary>
- /// <param name="row"></param>
- internal void LoadValue(MDataRow row)
- {
- if (row != null)
- {
- MDataCell myCell;
- foreach (MDataCell cell in row)
- {
- myCell = this[cell._CellStruct.ColumnName];
- if (myCell == null)
- {
- myCell = new MDataCell(ref cell._CellStruct);
- this.Add(myCell);
- }
- myCell._CellValue.Value = cell._CellValue.Value;
- myCell._CellValue.IsNull = cell._CellValue.IsNull;
- myCell._CellValue.IsChange = cell._CellValue.IsChange;
- }
- }
- }
- /// <summary>
- /// 转成实体
- /// </summary>
- /// <typeparam name="T">实体名称</typeparam>
- public T ToEntity<T>()
- {
- T obj = (T)Activator.CreateInstance(typeof(T));
- System.Reflection.PropertyInfo[] pis = obj.GetType().GetProperties();
- object propValue = null;
- MDataCell cell = null;
- for (int i = 0; i < pis.Length; i++)
- {
- cell = this[pis[i].Name];
- if (cell == null)
- {
- continue;
- }
- propValue = cell.Value;
- if (propValue == null || propValue == DBNull.Value)
- {
- continue;
- }
- pis[i].SetValue(obj, propValue, null);
- }
- return obj;
- }
- #region ICloneable 成员
- public MDataRow Clone()
- {
- MDataRow row = new MDataRow();
- for (int i = 0; i < base.Count; i++)
- {
- CellStruct mcb = base[i]._CellStruct;
- MDataCell mdc = new MDataCell(ref mcb);
- mdc.Value = base[i].Value;
- mdc._CellValue.IsChange = false;
- row.Add(mdc);
- }
- row.TableName = this.TableName;
- return row;
- }
- #endregion
- #region IDataRecord 成员
- int IDataRecord.FieldCount
- {
- get
- {
- return base.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)
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- 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)
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- string IDataRecord.GetDataTypeName(int i)
- {
- return "";
- //return this[i]._CellValue.ValueType.Name;
- }
- 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]._CellStruct.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 (string)this[i].Value;
- }
- int IDataRecord.GetOrdinal(string name)
- {
- return (int)this[name].Value;
- }
- string IDataRecord.GetString(int i)
- {
- return (string)this[i].Value;
- }
- object IDataRecord.GetValue(int i)
- {
- return this[i].Value;
- }
- int IDataRecord.GetValues(object[] values)
- {
- return 0;
- }
- bool IDataRecord.IsDBNull(int i)
- {
- return this[i].Value == DBNull.Value;
- }
- object IDataRecord.this[string name]
- {
- get
- {
- return this[name].Value;
- }
- }
- object IDataRecord.this[int i]
- {
- get
- {
- return this[i].Value;
- }
- }
- #endregion
- #region ICustomTypeDescriptor 成员
- public System.ComponentModel.AttributeCollection GetAttributes()
- {
- return null;
- }
- public string GetClassName()
- {
- return "MDataRow";
- }
- public string GetComponentName()
- {
- return "CYQ.Data";
- }
- public TypeConverter GetConverter()
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- public EventDescriptor GetDefaultEvent()
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- public PropertyDescriptor GetDefaultProperty()
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- public object GetEditor(Type editorBaseType)
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- public EventDescriptorCollection GetEvents(Attribute[] attributes)
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- public EventDescriptorCollection GetEvents()
- {
- throw new Exception(AppConst.Global_NotImplemented);
- }
- int index = 0;
- PropertyDescriptorCollection properties;
- public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
- {
- if (index == 1)
- {
- return properties;
- }
- index++;
- properties = new PropertyDescriptorCollection(null);
- foreach (MDataCell mdc in this)
- {
- properties.Add(new MDataProperty(mdc, null));
- }
- return properties;
- }
- public PropertyDescriptorCollection GetProperties()
- {
- return GetProperties(null);
- }
- public object GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd)
- {
- return this;
- }
- #endregion
- }
- }
|