DataTable and Generics

Source: Internet
Author: User

    The first time the machine room is reconstructed, the most used is the DataTable, when used to feel a little awkward, because the D layer from the database
After the data is removed, it is returned to the B-layer and the U-layer directly through the DataTable, which is not very safe for database data. And then slowly know
Generic, the problem of data security can be solved well by generics
First, the DataTable
       Pre name= "code" class= "CSharp" > public Function Querycard (card as entity.cardentity) as Entity.cardentity Implements ICar D.querycard Dim helper As New SqlHelper () ' Instantiation of SqlHelper object for helper Dim dt as DataTable ' received from sq The DataTable returned in Lhelper Dim cardinfo As Entity.cardentity = New entity.cardentity () Dim Query As String = "Sele CT * from Card_info where [email protected] "Storage SQL field <span style=" White-space:pre "></span><pre nam E= "code" class= "CSharp" > "Instantiate SqlParameter, and pass in Parameters 
Dim sqlparams as SqlParameter () = {New SqlParameter ("@cardno", card. Cardno)}  
                dt = Helper. Execselect (Query, sqlparams)        ' Invoke Object helper method        ' puts data in the DataTable into the corresponding field in the entity Cardinfo        Try            Cardinfo.cardno = dt. Rows (0) (0)            cardinfo.dates = dt. Rows (0) (5)            cardinfo.time = dt. Rows (0) (6)            cardinfo.status = dt. Rows (0) (7)            cardinfo.ischeck = dt. Rows (0) (8)            cardinfo.explain = dt. Rows (0) (9)            cardinfo.cardtype = dt. Rows (0) (1)   = dt. Rows (0) (2)            cardinfo.studentno = dt. Rows (0) (3)        Catch ex as Exception            ' error after the prompt            MsgBox ("card number is not present or error", CType (vbOKOnly + msgboxstyle.exclamation , MsgBoxStyle), "hint")        end Try        return cardinfo    ' return entity    End Function
      This is the data in the DataTable is directly assigned to the entity in the D layer, personally think it would be relatively good, because from the database
  The data obtained will not be returned directly to the U-layer and B-layer, but the obtained DataTable can also be returned to the U-layer B layer directly on the D-layer, then the U-layer
  Manipulating data in a DataTable is clearly not safe for data, and it is transmitted through a DataTable between layer three
  The data will also increase the coupling of three layers
Second, generic type
    ' DataTable converted to generic public Shared Function convertlist (Of T as {New}) (ByVal DT as DataTable) as IList (Of T) Dim MyList as New List (Of T) ' defines the returned collection Dim Mytype As Type = GetType (T) ' Get the type name Dim Dr As DataRow ' defines the rowset for the DataTable Dim tmpname as String = String.Empty ' variable initialization, which defines the empty The TEMP variable ' iterates through all the rows of the DataTable for every Dr in Dt. Rows Dim MyT As New T ' creates object for entity class Dim Propertys () as PropertyInfo = Myt.getty PE (). GetProperties () ' Define attribute collection Dim PR as PropertyInfo ' PropertyInfo ' instantiate ' traverse PR object all genera in the collection Propertys tmpname = pr. The name ' property names are assigned to the defined temporary variable ' to check if the DataTable contains this property if it contains a continuation of the IF (dt. Columns.contains (tmpname)) then If (pr.                  CanWrite = False) Then ' determines whether the property is writable if it is not writable and jumps out of this loop Continue for  End if Dim value As Object = DR (Tmpname) ' defines the variable of the object type to hold the value of the column ' If not NULL, assign a value to the object property If (value.tostring <> DBNull.Value.ToString ()) then PR.            SetValue (MyT, value, nothing) ' dynamically accesses object properties by reflection and assigns the value End If End If Next Mylist.add (MyT) ' adds the acquired attribute to the collection Next return MyList ' Returns the collection End Function
  "D layer calls the method directly   
       Pre name= "code" class= "CSharp" > public Function selectstudent (ByVal student as entity.studententity) as IList (of Entity .        studententity) Implements Iselectstudent.selectstudent Dim Helper As New SqlHelper Dim dt As DataTable Dim sql As String = "SELECT * from Student_info where [email protected]" Dim stuinfo As List (of Entity.stude ntentity) <span style= "White-space:pre" ></span> Dim para as SqlParameter () = {New SqlParameter ("@student No ", student. STUDENTNO)} 
<span style= "White-space:pre" ></span> ' Call the SqlHelper method dt = Helper. Execselect (SQL, para) <span style= "White-space:pre" ></span> "DataTable type conversion to generic Stuinfo = Entity.genericity . Convertlist (of Entity.studententity) (DT) Return stuinfo End Function 

     This allows for decoupling between B-and D-layers through generic transformations, and the type constraints of generic definitions can also largely guarantee that data is
  The entire sex.
Third, summary
     When using generics, be sure to note the data type, the data type in the entity and the data type of the database must be kept
     In the use of some of their own useless technology, very helpful to themselves.

DataTable and Generics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.