'********************************************** '说明:将DataTable转化为泛型集合 '命名空间:DAL '机器名称:晓 '创建日期:2015/2/23 11:13:28 '作者:郑浩 '版本号:V1.00 '********************************************** Imports System.Collections.Generic '增加泛型的命名空间 Imports System.Reflection '引入反射:为了使用PropertyInfo Public Class ConvertGenericsHelper '将datatable转化为泛型集合 Public Shared Function convertToList(Of T As {New})(ByVal dt As DataTable) As IList(Of T) '注意:convertToList(Of T As {New}) 这里的new是用来约束T的,必须有,不然new T的时候会出现错误 Dim myList As New List(Of T) '定义最终返回的集合 Dim myTpye As Type = GetType(T) '得到实体类的类型名 Dim dr As DataRow '定义行集 Dim tempName As String = String.Empty '定义一个临时变量 '遍历DataTable的所有数据行 For Each dr In dt.Rows Dim myT As New T '定义一个实体类的对象 Dim propertys() As PropertyInfo = myT.GetType().GetProperties() '定义属性集合 Dim Pr As PropertyInfo '遍历该对象的所有属性 For Each Pr In propertys tempName = Pr.Name '将属性名称赋值给临时变量 '检查DataTable是否包含此列(列名==对象的属性名) If (dt.Columns.Contains(tempName)) Then '将此属性与datatable里的列明比较,查看datatable是否包含此属性 '判断此属性是否有Setter If (Pr.CanWrite = False) Then '判断此属性是否可写,如果不可写,跳出本次循环 Continue For End If Dim value As Object = dr(tempName) '定义一个对象型的变量来保存列的值 If (value.ToString <> DBNull.Value.ToString()) Then '如果非空,则赋给对象的属性 Pr.SetValue(myT,value,Nothing) '在运行期间,通过反射,动态的访问一个对象的属性 End If End If Next myList.Add(myT) '添加到集合 Next Return myList '返回实体集合 End Function End Class
D层代码
'********************************************** '说明:利用泛型集合来查看学生余额 '命名空间:DAL '机器名称:晓 '创建日期:2015/2/23 11:27:21 '作者:郑浩 '版本号:V1.00 '********************************************** Imports IDAL Imports System.Data.sqlClient Public Class sqlserverCheckBalanceDAL : Implements ICheckBalance Public Function ICheckBalance(studentinfo As Entity.EN_StudentInfo) As List(Of Entity.EN_StudentInfo) Implements ICheckBalance.ICheckBalance Dim sql As String sql = "select * from ZH_StudentInfo where CardNo = @CardNo" Dim paras As sqlParameter() = {New sqlParameter("CardNo",studentinfo.CardNo)} Dim dt As New DataTable '保存转换后的泛型集合 Dim myList As New List(Of Entity.EN_StudentInfo) dt = sqlHelper.sqlHelper.ExecSelect(sql,CommandType.Text,paras) '先判断dt是否为空 If dt.Rows.Count > 0 Then '将dt转换为泛型集合 myList = ConvertGenericsHelper.convertToList(Of Entity.EN_StudentInfo)(dt) Return myList Else Return Nothing End If End Function End Class
’直接给文本框赋值
Dim studentinfo As New Entity.EN_StudentInfo studentinfo.CardNo = Trim(txtCardNo.Text) Dim dt As New List(Of Entity.EN_StudentInfo) Dim checkbalance As New Facade.FacadeCheckBalance dt = checkbalance.CheckBalance(studentinfo) txtClass.Text = dt(0).Classes '0表示泛型集合中的第一个实体 txtStudentNo.Text = dt(0).StudentNo txtName.Text = dt(0).StudentName txtSex.Text = dt(0).Sex txtStatus.Text = dt(0).Status txtGrade.Text = dt(0).Grade txtBalance.Text = dt(0).Cash txtExplain.Text = dt(0).Explain End Sub
’和DataGridView控件交互
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click Dim studentinfo As New Entity.EN_StudentInfo studentinfo.CardNo = Trim(txtCardNo.Text) Dim dt As New List(Of Entity.EN_StudentInfo) Dim checkbalance As New Facade.FacadeCheckBalance dt = checkbalance.CheckBalance(studentinfo) dgvRecord.DataSource = dt dgvRecord.AutoGenerateColumns = True dgvRecord.Columns(1).HeaderText = "卡号" End Sub