Advertisement
NAK

ExecuteSqlDataReader Module1 (VB.NET)

NAK
Dec 3rd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.98 KB | None | 0 0
  1. Imports System.Data.SqlClient
  2. Imports System.Runtime.Serialization
  3.  
  4. Module Module1
  5.  
  6.     Sub Main()
  7.         ' not using any Params for this example but left it in
  8.         Dim Params As New List(Of Parameter)()
  9.         ' Get our first list of data
  10.         Dim testlist1 = New List(Of TestClass_1)()
  11.         testlist1 = ExecuteSqlDataReader(Of TestClass_1)("<your SQL query here>", "<your SQL connection here>", Params)
  12.         ' Get our second list of data
  13.         Dim testlist2 = New List(Of TestClass_2)()
  14.         testlist2 = ExecuteSqlDataReader(Of TestClass_2)("<your SQL query here>", "<your SQL connection here>", Params)
  15.     End Sub
  16.  
  17.     ''' <summary>
  18.     '''  Generic function that uses SqlClient to connect and retrieve data from a DB
  19.     ''' </summary>
  20.     ''' <typeparam name="T"></typeparam>
  21.     ''' <param name="SQLText"></param>
  22.     ''' <param name="SQLConnectionStrong"></param>
  23.     ''' <param name="params"></param>
  24.     ''' <returns>Return a collection of T></returns>
  25.     ''' <remarks></remarks>
  26.     Public Function ExecuteSqlDataReader(Of T)(SQLText As String, SQLConnectionStrong As String, params As List(Of Parameter)) As List(Of T)
  27.  
  28.         Dim ListOfT As List(Of T) = New List(Of T)
  29.  
  30.         Using SQLcon = New SqlConnection(SQLConnectionStrong)
  31.  
  32.             Using SQLCMD As New SqlCommand(SQLText, SQLcon)
  33.  
  34.                 SQLCMD.CommandText = SQLText          ' set the SQL command (SP or in line SQL)
  35.                 SQLCMD.CommandType = CommandType.Text       ' set the command type (StoredProcedure or Text)
  36.                 'SQLCMD.CommandTimeout = mySQLSection.CommandTimeout ' set the command time out
  37.  
  38.                 For Each p In params
  39.                     SQLCMD.Parameters.AddWithValue(p.Name, p.Value)
  40.                 Next
  41.  
  42.                 If SQLcon.State = ConnectionState.Closed Then
  43.                     SQLcon.Open() ' open the connection
  44.                 End If
  45.  
  46.                 Using myReader As SqlDataReader = SQLCMD.ExecuteReader()                     ' create the Data Reader and get the Data
  47.  
  48.                     Dim dt As DataTable = myReader.GetSchemaTable()         ' Get the Table Schema (Needed to identify each column type)
  49.  
  50.                     Do While myReader.Read()        ' for each record in the data set
  51.  
  52.                         Dim tmp = CreateInstanceOf_T(Of T)() ' create an instance of T
  53.                         Dim index As Integer = 0    ' reset the column indexer
  54.  
  55.                         For Each row As DataRow In dt.Rows                              ' for each column in the table
  56.  
  57.                             Dim v = GetType(T).GetProperty(row(0).ToString())    ' get the type
  58.                             Try
  59.                                 If v <> Nothing Then
  60.  
  61.                                     Select Case DirectCast(v.PropertyType, System.Type).FullName ' identify the Column data type and convert the contents
  62.                                         Case "System.Int32"
  63.                                             GetType(T).GetProperty(row(0).ToString()).SetValue(tmp, myReader.GetInt32(index))       ' to Int32
  64.                                         Case "System.Guid"
  65.                                             GetType(T).GetProperty(row(0).ToString()).SetValue(tmp, myReader.GetGuid(index))        ' to Guid
  66.                                         Case "System.String"
  67.                                             If Not IsDBNull(myReader(index)) Then
  68.                                                 GetType(T).GetProperty(row(0).ToString()).SetValue(tmp, myReader.GetString(index))      ' to String
  69.                                             End If
  70.                                         Case "System.Boolean"
  71.                                             GetType(T).GetProperty(row(0).ToString()).SetValue(tmp, myReader.GetBoolean(index))     ' to Boolean
  72.                                         Case "System.Decimal"
  73.                                             GetType(T).GetProperty(row(0).ToString()).SetValue(tmp, myReader.GetDecimal(index))     ' to Decimal
  74.                                             ' todo :: complete the convertion for oter types
  75.                                         Case Else
  76.                                     End Select
  77.  
  78.                                 End If
  79.  
  80.                             Catch ex As Exception
  81.                                 ' todo :: should log any errors here
  82.                             End Try
  83.                             index = index + 1   ' increment the column indexer
  84.                         Next
  85.                         ListOfT.Add(tmp)        ' add the temp object (T) to the ListOfT
  86.                     Loop
  87.                 End Using
  88.             End Using
  89.  
  90.         End Using
  91.  
  92.         Return ListOfT      ' return the list of T objects
  93.  
  94.     End Function
  95.  
  96.     Public Function CreateInstanceOf_T(Of T)() As T
  97.         Dim tmp As T = GetType(T).GetConstructor(New System.Type() {}).Invoke(New Object() {})
  98.         Return tmp
  99.     End Function
  100.  
  101. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement