-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultSet.cs
172 lines (157 loc) · 4.49 KB
/
ResultSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
namespace Crster.Database.Helper
{
[DebuggerStepThrough()]
public class ResultSet
{
private int _Index;
public DataTable DataTable { get; internal set; }
public int RecordCounts
{
get
{
return DataTable.Rows.Count;
}
}
public ResultSet(string resultName)
{
_Index = -1;
DataTable = new DataTable(resultName);
}
public ResultSet(DataTable data)
{
_Index = -1;
if (data == null) {
throw new ArgumentNullException("data");
}
DataTable = data;
}
public void ResetIndex()
{
_Index = -1;
}
public bool Read()
{
if (DataTable == null) {
return false;
}
if (_Index >= (DataTable.Rows.Count - 1)) {
return false;
}
else{
_Index++;
return true;
}
}
public T Value<T>(string column)
{
object ret = ConvertDbNullToNull(column);
return (T)ret;
}
private object ConvertDbNullToNull(string column)
{
object ret = DataTable.Rows[_Index][column];
if (ret == DBNull.Value) {
ret = null;
}
return ret;
}
public bool HasValue(string column)
{
return DataTable.Rows[_Index][column] != DBNull.Value;
}
public bool GetBoolean(string column)
{
return Convert.ToBoolean(ConvertDbNullToNull(column));
}
public byte GetByte(string column)
{
return Convert.ToByte(ConvertDbNullToNull(column));
}
public sbyte GetSbyte(string column)
{
return Convert.ToSByte(ConvertDbNullToNull(column));
}
public short GetShort(string column)
{
return Convert.ToInt16(ConvertDbNullToNull(column));
}
public ushort GetUshort(string column)
{
return Convert.ToUInt16(ConvertDbNullToNull(column));
}
public int GetInteger(string column)
{
return Convert.ToInt32(ConvertDbNullToNull(column));
}
public uint GetUinteger(string column)
{
return Convert.ToUInt32(ConvertDbNullToNull(column));
}
public long GetLong(string column)
{
return Convert.ToInt64(ConvertDbNullToNull(column));
}
public ulong GetUlong(string column)
{
return Convert.ToUInt64(ConvertDbNullToNull(column));
}
public float GetFloat(string column)
{
return Convert.ToSingle(ConvertDbNullToNull(column));
}
public double GetDouble(string column)
{
return Convert.ToDouble(ConvertDbNullToNull(column));
}
public decimal GetDecimal(string column)
{
return Convert.ToDecimal(ConvertDbNullToNull(column));
}
public DateTime GetDateTime(string column)
{
return Convert.ToDateTime(ConvertDbNullToNull(column));
}
public string GetString(string column)
{
return Convert.ToString(ConvertDbNullToNull(column));
}
public string GetFromHash(string colum)
{
string ret = null;
string result = GetString(colum);
if (result != "") {
byte[] arr = Convert.FromBase64String(result);
ret = Encoding.Default.GetString(arr);
}
return ret;
}
public static string GetHash(string data)
{
string ret = null;
try {
byte[] arr = Encoding.Default.GetBytes(data);
ret = Convert.ToBase64String(arr);
}
catch{
ret = null;
}
return ret;
}
public T EnumValue<T>(string column)
{
object value = ConvertDbNullToNull(column);
if(value.GetType() == typeof(string)){
return (T)Enum.Parse(typeof(T), value as string);
}
else{
return (T)Enum.ToObject(typeof(T), value);
}
}
}
}