DataHelper在使用过程中最好的就配合BindObject类一起使用。下面附上BindObject类的源码:
BindObject
1
public class BindObject
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
绑定#region 绑定
4
5
public static object[] BindObjectToArray(Type instanceType, IDataReader reader)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
try
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
PropertyInfo [] properties = instanceType.GetProperties();
10
ArrayList items = new ArrayList();
11
Hashtable fieldName = new Hashtable();
12
int fieldCount = reader.FieldCount;
13
for (int i = 0; i < fieldCount; i++)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
string name = reader.GetName(i).ToString().ToLower();
16
fieldName.Add(name, i);
17
}
18
while (reader.Read())
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
object instance = Activator.CreateInstance(instanceType);
21
foreach (PropertyInfo property in properties)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
object[] attributes = property.GetCustomAttributes(typeof(MappingFieldAttribute), false);
24
object[] encoders = property.GetCustomAttributes(typeof(CharacterEncodeAttribute), false);
25
string aName = string.Empty;
26
CharacterEncodeAttribute encoder = null;
27
if(attributes != null && attributes.Length > 0)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
MappingFieldAttribute mapping = attributes[0] as MappingFieldAttribute;
30
aName = mapping.FieldName.ToLower();
31
}
32
if (encoders != null && encoders.Length > 0)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
encoder = encoders[0] as CharacterEncodeAttribute;
35
}
36
string pName = property.Name.Trim().ToLower();
37
int index = -1;
38
if (fieldName.ContainsKey(pName))
39
index = (int)fieldName[pName];
40
else if (aName != string.Empty && fieldName.ContainsKey(aName))
41
index = (int)fieldName[aName];
42
if (index >= 0)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
object obj = reader[index];
45
Type propertyType = property.PropertyType;
46
if (propertyType.IsArray || propertyType.IsInterface)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
continue;
49
}
50
else if (property.PropertyType.IsEnum)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
if(obj != null && obj != DBNull.Value)
53
property.SetValue(instance, Enum.Parse(property.PropertyType, obj.ToString(), true), null);
54
}
55
else
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
if(obj != null && obj != DBNull.Value)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
if(property.PropertyType == typeof(string) && encoder != null)
60
property.SetValue(instance, encoder.Encode(obj.ToString()), null);
61
else
62
property.SetValue(instance, Convert.ChangeType(obj, property.PropertyType), null);
63
}
67
}
68
}
69
}
70
items.Add(instance);
71
}
72
if (!reader.IsClosed)
73
reader.Close();
74
object [] instances = (object[])Array.CreateInstance(instanceType, items.Count);
75
for(int i = 0; i < items.Count; i++)
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
//string str= "<![CDATA[" + items[i].ToString() +"]]>";
78
instances[i] = items[i];
79
}
80
return instances;
81
}
82
catch(Exception ex)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
throw ex;
85
}
86
}
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
90
/// 绑定
91
/// </summary>
92
/// <param name="instanceType"></param>
93
/// <param name="reader"></param>
94
/// <returns></returns>
95
public static IList BindObjectToInstance(Type instanceType, IDataReader reader)
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
PropertyInfo [] properties = instanceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
98
ArrayList items = new ArrayList();
99
Hashtable fieldName = new Hashtable();
100
int fieldCount = reader.FieldCount;
101
for (int i = 0; i < fieldCount; i++)
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
string name = reader.GetName(i).ToString().ToLower();
104
fieldName.Add(name, i);
105
}
106
while (reader.Read())
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
object instance = Activator.CreateInstance(instanceType);
109
foreach (PropertyInfo property in properties)
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
string pName = property.Name.Trim().ToLower();
112
int index = -1;
113
if (fieldName.ContainsKey(pName))
114
index = (int)fieldName[pName];
115
if (index >= 0)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
object obj = reader[index];
118
Type propertyType = property.PropertyType;
119
if (propertyType.IsArray || propertyType.IsInterface)
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
continue;
122
}
123
else if (property.PropertyType.IsEnum)
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
if(obj != null && obj != DBNull.Value)
126
property.SetValue(instance, Enum.Parse(property.PropertyType, obj.ToString(), true), null);
127
}
128
else
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
if(obj != null && obj != DBNull.Value)
131
property.SetValue(instance, Convert.ChangeType(obj, property.PropertyType), null);
132
}
133
}
134
}
135
items.Add(instance);
136
}
137
if (!reader.IsClosed)
138
reader.Close();
139
return items;
140
}
141
#endregion
142
}
下面再给出一个结合两者使用的方法:
Test(int id)
1
public IList Test(int id)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
using (DataHelper helper = new DataHelper("server=localhost;database=pubs;uid=sa;pwd=111;"))
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
IDataReader reader = helper.CreateQuery(@"Select * From Table1 Where id=:id")
6
.SetInt32("id", id)
7
.ExecuteReader();
8
return BindObject.BindObjectToInstance(typeof(Table1), reader);
9
}
10
}
这个操作DataHelper的方法比较简单一看就明白我这里就不详细介绍了,简单的介绍下BindObject类,它是运用了反射的方式来对实体中的字段进行读取比较,然后赋值的,所以实体中的字段必须对应我们所写sql中的列。
个人觉得这些都没啥技术含量,其实真正重要是思想,是一种解决问题的方法。
注:这里的BindObject类里还涉及到自定义属性的问题,直接拷贝进去是运行不了,在下篇里将继续附上该类代码。并介绍其用处。呵呵。。。
本人水平有限,希望能迎来大家的“砖头”,越多越好咯。。。