![]() | CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
MDataTable 原理解析 数据控件绑定-下篇
Principles |
|
|
| #TopicOwner |
还记得当初以为似找到:行数组Copy之后,再array.GetEnumerator();就可行。
实际操作之后,发现不可行,于是,这不可行的路就不写了,避免浪费大伙精力看了。
以下讲可行之路: 通过Reflector找到SqlDataReader类,因为它也是可绑定之一的数据源,虽然直拉绑定往往造成链接未关闭事件。 通过研究: public class SqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord 发现其继承自DbDataReader,于是,点进DbDataReader: public abstract class DbDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable 这里,我们的MDataTable只要继承自中间的IDataReader, IEnumerable两个即可,就可以实现绑定之路了。 继承之后,当然就是要实现其它接口了: #region IDataReader 成员
public void Close() { _Mdr.Clear(); } public int Depth { get { if (_Mdr != null) { return _Mdr.Count; } return 0; } } public DataTable GetSchemaTable() { return null; } public bool IsClosed { get { return true; } } public bool NextResult() { if (_Ptr < _Mdr.Count - 1) { return true; } else { return false; } } public bool Read() { if (_Ptr < _Mdr.Count) { _Ptr++; return true; } else { return false; } } public int RecordsAffected { get { return -1; } } #endregion #region IDataRecord 成员 private int _Ptr = 0; public int FieldCount { get { if (this.Columns != null) { return this.Columns.Count; } return 0; } } public bool GetBoolean(int i) { return (bool)_Mdr[_Ptr][i].Value; } public byte GetByte(int i) { return (byte)_Mdr[_Ptr][i].Value; } public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) { throw new Exception("The method or operation is not implemented."); } public char GetChar(int i) { return (char)_Mdr[_Ptr][i].Value; } public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) { throw new Exception("The method or operation is not implemented."); } public IDataReader GetData(int i) { return this; } public string GetDataTypeName(int i) { return DataType.GetDbTypeFromSqlDbType(_Mdr[_Ptr].Columns[i].SqlType.ToString()).ToString(); } public DateTime GetDateTime(int i) { return (DateTime)_Mdr[_Ptr][i].Value; } public decimal GetDecimal(int i) { return (decimal)_Mdr[_Ptr][i].Value; } public double GetDouble(int i) { return (double)_Mdr[_Ptr][i].Value; } public Type GetFieldType(int i) { return Type.GetType("System." + GetDataTypeName(i)); } public float GetFloat(int i) { return (float)_Mdr[_Ptr][i].Value; } public Guid GetGuid(int i) { return (Guid)_Mdr[_Ptr][i].Value; } public short GetInt16(int i) { return (short)_Mdr[_Ptr][i].Value; } public int GetInt32(int i) { return (int)_Mdr[_Ptr][i].Value; } public long GetInt64(int i) { return (long)_Mdr[_Ptr][i].Value; } public string GetName(int i) { return _Mdr[_Ptr][i].ColumnName; } public int GetOrdinal(string name) { throw new Exception("The method or operation is not implemented."); } public string GetString(int i) { return _Mdr[_Ptr][i].ColumnName.ToString(); } public object GetValue(int i) { return null; //return _Mdr[_Ptr][i-1].Value; } public int GetValues(object[] values) { for (int i = 0; i < values.Length; i++) { values[i] = _Mdr[_Ptr - 1][i].Value; } return values.Length; } public bool IsDBNull(int i) { return _Mdr[_Ptr][i].IsNull; } public object this[string name] { get { return null; } } public object this[int i] { get { return _Mdr[i]; } } #endregion #region IEnumerable 成员 public IEnumerator GetEnumerator() { return new System.Data.Common.DbEnumerator(this); } #endregion 至此,MDataTable已经可以替代常用的DataTable了。 同样具有常见的行,列,数据结构,绑定控件等功能。 ![]() |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.