0
|
1 ???using System; |
|
2 using System.Collections; |
|
3 using System.Collections.Generic; |
|
4 using System.Data; |
|
5 using System.Data.Common; |
|
6 |
|
7 namespace UCIS { |
|
8 public class DBReader : IEnumerable, IEnumerator { |
|
9 private IDbCommand _Command; |
|
10 private IDataReader _Reader; |
|
11 private object[] _CurrentRow; |
|
12 |
|
13 internal DBReader(IDbCommand Command) { |
|
14 _Command = Command; |
|
15 } |
|
16 |
|
17 public bool Read() { |
|
18 if (_Reader == null) { |
|
19 if (_Command == null) return false; |
|
20 _Reader = _Command.ExecuteReader(); |
|
21 } |
|
22 if (_Reader.Read()) { |
|
23 return true; |
|
24 } else { |
|
25 Close(); |
|
26 return false; |
|
27 } |
|
28 } |
|
29 |
|
30 public void Close() { |
|
31 if (_Reader != null) _Reader.Close(); |
|
32 if (_Command != null) { |
|
33 _Command.Connection.Close(); |
|
34 _Command.Dispose(); |
|
35 } |
|
36 _Command = null; |
|
37 _CurrentRow = null; |
|
38 _Reader = null; |
|
39 } |
|
40 |
|
41 public object GetField() { |
|
42 if (_Reader == null) { |
|
43 return _Command.ExecuteScalar(); |
|
44 } else { |
|
45 return _Reader.GetValue(0); |
|
46 } |
|
47 } |
|
48 |
|
49 public object GetField(int Offset) { |
|
50 if (_Reader == null) Read(); |
|
51 return _Reader.GetValue(Offset); |
|
52 } |
|
53 public object[] GetRow(bool GoNextRow) { |
|
54 object[] Result = null; |
|
55 if (_Reader == null) { |
|
56 if (!Read()) return null; |
|
57 } |
|
58 Result = new object[_Reader.FieldCount]; |
|
59 |
|
60 _Reader.GetValues(Result); |
|
61 if (GoNextRow) Read(); |
|
62 return Result; |
|
63 } |
|
64 public object[] GetRow() { |
|
65 return GetRow(true); |
|
66 } |
|
67 public object[][] GetAllRows() { |
|
68 List<object[]> Result = new List<object[]>(); |
|
69 object[] ResultArray = null; |
|
70 if (_Reader == null) { |
|
71 if (!Read()) return null; |
|
72 } |
|
73 do { |
|
74 ResultArray = new object[_Reader.FieldCount]; |
|
75 |
|
76 _Reader.GetValues(ResultArray); |
|
77 Result.Add(ResultArray); |
|
78 } |
|
79 while (_Reader.Read()); |
|
80 Close(); |
|
81 return Result.ToArray(); |
|
82 } |
|
83 |
|
84 IEnumerator IEnumerable.GetEnumerator() { |
|
85 return this; |
|
86 } |
|
87 object IEnumerator.Current { |
|
88 get { return _CurrentRow; } |
|
89 } |
|
90 bool IEnumerator.MoveNext() { |
|
91 if (!Read()) return false; |
|
92 _CurrentRow = GetRow(false); |
|
93 return true; |
|
94 } |
|
95 void IEnumerator.Reset() { |
|
96 throw new NotImplementedException(); |
|
97 } |
|
98 |
|
99 public void Dispose() { |
|
100 Close(); |
|
101 } |
|
102 } |
|
103 } |