(NET) NET (2022)

Access to SqlLite converter. VB example of Generic, Func, Extension. Using MS.ACE.OLEDB provider and SQLite.

This is simplest console application, I have publish is to https://github.com/Alex-1557/Access2SQLiteConverter.


Maybe for beginners can be interesting how I use Relection, Func, Generic. Please look to my code.


   1:  Imports System.Data.Common
   2:  Public Module RawSqlQuery
   3:      <Runtime.CompilerServices.Extension>
   4:      Public Function RawSqlQuery(Of T)(Connection As OleDb.OleDbConnection, ByVal SqlQuery As String, ByVal RowMapperFunc As Func(Of OleDb.OleDbDataReader, T), Optional NeedPrint As Boolean = True) As List(Of T)
   5:          Try
   6:              If NeedPrint Then
   7:                  Dim Prop1 = GetType(T).GetProperties()
   8:                  Dim Res1 As New List(Of String)
   9:                  Prop1.ToList.ForEach(Sub(Y) Res1.Add(Y.Name))
  10:                  Console.WriteLine(String.Join("#", Res1))
  11:              End If
  12:   
  13:              Using Command = New OleDb.OleDbCommand
  14:                  Command.CommandText = SqlQuery
  15:                  Command.CommandType = CommandType.Text
  16:                  Command.Connection = Connection
  17:   
  18:                  Using RDR = Command.ExecuteReader()
  19:                      Dim ResultList = New List(Of T)()
  20:   
  21:                      While RDR.Read()
  22:   
  23:                          Dim X = RowMapperFunc(RDR)
  24:                          ResultList.Add(X)
  25:   
  26:                          If NeedPrint Then
  27:                              Dim Prop2 = X.GetType().GetProperties()
  28:                              Dim Res2 As New List(Of String)
  29:                              Prop2.ToList.ForEach(Sub(Y)
  30:                                                       If Y.PropertyType.Name = "Byte[]" Then
  31:                                                           Res2.Add("*BYTE ARRAY*")
  32:                                                       Else
  33:                                                           Res2.Add(Y.GetValue(X, Nothing))
  34:                                                       End If
  35:   
  36:                                                   End Sub)
  37:                              Console.WriteLine(String.Join("#", Res2))
  38:                          End If
  39:   
  40:                      End While
  41:   
  42:                      Return ResultList
  43:                  End Using
  44:              End Using
  45:          Catch ex As Exception
  46:              Console.WriteLine($" Error: {ex.Message} : in {SqlQuery}")
  47:              Return Nothing
  48:          End Try
  49:      End Function
  50:   
  51:  End Module

   1:  Module CheckDBNull
   2:      <Runtime.CompilerServices.Extension>
   3:      Public Function CheckDBNull(Of T)(ByVal DbField As Object) As T
   4:          If DbField Is Nothing OrElse DbField Is DBNull.Value Then
   5:              Return Nothing
   6:          Else
   7:              Return CType(DbField, T)
   8:          End If
   9:   
  10:      End Function
  11:   
  12:  End Module


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: http://www.vb-net.com/AtiConverter/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>