学生查询充值记录之二——DataTable转型实体集

         之前写过,D层与U层的关系,因为DataTable关系更加密切了。为了使得他俩和平分手(解耦合),就是用了泛型——充当棒打鸳鸯的角色。在D层吧DatatTable转换成单个的实体,然后再把实体填充到泛型集合众中。

        实体类就是对数据库的映射,所以应该将实体类中德属性和数据库表中的字段是相对应的,把DataTable中的记录看作是一个实体,然后把其中的字段读出来,在放在实体类的属性中,这样,再把对应实体类中存在泛型集合中。DataTable中有多少的记录,在泛型集合众就有多少个实体类。切记的一点就是,实体类的属性一定要和数据库中字段对应,以及属性的返回类型都要和数据库中的对应起来。

       对于这个由通过实体类填充泛型集合来代替DataTable的方法,我是写在了SqlHelper中,因为就感觉他和SqlHelper中德其他方法一样,都是公用的,被封装起来的!

代码如下:

SQLHelper

 Public Sub dataTableToList(Of T As New)(dt As DataTable, ts As List(Of T))


        '定义一个实体集
        ' Dim ts As List(Of T) = New List(Of T)()
        '获得此模型的类型
        Dim type As Type = GetType(T)
        ' 定义一个临时变量()
        Dim tempName As String = String.Empty
        '便利DataTable中所有的数据行
        For Each dr As DataRow In dt.Rows

            Dim m As T = If((Nothing Is Nothing), Activator.CreateInstance(Of T)(), Nothing)
            '获得此模型的公共属性
            Dim propertys As PropertyInfo() = m.[GetType]().GetProperties()
            '便利该对象的所有属性
            Dim array As PropertyInfo() = propertys
            Dim i As Integer = 0
            While i < array.Length
                Dim pi As PropertyInfo = array(i)
                tempName = pi.Name  '将属性名称赋值给临时变量
                '检查datattable是否包含此列   列名==对象的属性名
                If dt.Columns.Contains(tempName) Then
                    '判断此属性是否有setter
                    If pi.CanWrite Then '该属性不可写直接跳出
                        Dim value As Object = dr(tempName)
                        '如果非空,则付给对象的属性
                        If value IsNot DBNull.Value Then
                            pi.SetValue(m, value, Nothing)
                        End If
                    End If
                End If
                i += 1
                Continue While
            End While
            '添加对象到泛型集合中()
            ts.Add(m)
        Next
    End Sub


D层

   Public Function IInquireCharge(ByVal card As cardReChargeEntity) As IList(Of cardReChargeEntity) Implements IDAL.ICard.IInquireCharge
        Try
            Dim cmdText As String = "Select * from T_CardRecharge where cardId=@cardId "
            Dim sqlParameters As SqlParameter()
            Dim dt As DataTable

            '实例化
            Dim cardRecharge As New List(Of Entity.cardReChargeEntity)
            sqlParameters = {New SqlParameter("@cardId", card.cardId)}
            '显示调用SQLHelper返回datatable
            dt = sqlHelper.ExecSelect(cmdText, sqlParameters)

            '调用EntitySet方法,使得datatable转化为实体集
            entitySet.dataTableToList(dt, cardRecharge)

            '返回转换后的实体集
            Return cardRecharge
        Catch ex As Exception
            Throw ex
        End Try
    End Function

B层

 

 '查询学生的充值记录,在表CardCharge中
    Public Function InquireCharge(ByVal card As Entity.cardReChargeEntity) As IList(Of cardReChargeEntity)
        Try
            Dim iInquireCharge As IDAL.ICard
            iInquireCharge = factory.CreateCardInfo
            Return iInquireCharge.IInquireCharge(card)
        Catch ex As Exception
            Throw ex
        End Try


    End Function

U层

 Private Sub butOk_Click(sender As Object, e As EventArgs) Handles butOk.Click
        Dim card As New Entity.cardReChargeEntity
        Dim mgr As New BLL.CardBLL
        Dim data As IList
        card.cardId = CInt(txtCardId.Text)
        data = CType(mgr.InquireCharge(card), IList)
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = data

    End Sub

       这样就是通过实体类填充泛型集合,实现了学生查询充值记录。通过做机房收费系统,感觉很神奇,学到的越来越多,想到的也越来越多了!

       这次给我感触最深的就是:之前写代码上来就说要写SQLHelper,之后又吵着想想怎么不返回DataTable了,但是这样做最终自己只是把时间浪费在了一些自己看不懂的资料上,为什么不一步一步的去做呢,当你有了需求,做到一定的时候,那时候再去做这些工作会更好的!

      公路不是从来就有的,当人们学会行走的时候,便有了一条小径,之后走的人多了,小径变成了一条路,之后技术发达了,有了各种汽车之后,便开始有了公路!!脚踏实地,勿想一步登天,否则会摔的很惨!

你可能感兴趣的:(机房收费系统)