Advertisement
SEMO_Pa3x

njdb

May 29th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.13 KB | None | 0 0
  1. Imports System.Data
  2.  
  3. Public Class njdb
  4.     ' njdb v2.0 bY njq8
  5.     Private WithEvents W As DataTable
  6.     Public ReadOnly Path As String = ""
  7.     Sub New(ByVal PathToDb As String)
  8.  
  9.         Path = PathToDb
  10.         If IO.File.Exists(Path) Then
  11.             Try
  12.                 Dim r As New Data.DataSet
  13.                 r.ReadXml(Path)
  14.                 W = r.Tables(0)
  15.                 r.Dispose()
  16.                 Exit Sub
  17.             Catch ex As Exception
  18.             End Try
  19.         End If
  20.         W = New Data.DataTable("njdb")
  21.     End Sub
  22.     Public Sub Dispose()
  23.         W.Dispose()
  24.         W = Nothing
  25.     End Sub
  26.     Public Sub Save()
  27.         W.WriteXml(Path)
  28.     End Sub
  29.     Public Sub AddColumn(ByVal Name As String, ByVal Unique As Boolean)
  30.         For Each x In Columns()
  31.             If x.ToLower = Name.ToLower Then
  32.                 Exit Sub
  33.             End If
  34.         Next
  35.         W.Columns.Add(Name).Unique = Unique
  36.         Save()
  37.     End Sub
  38.     Public Sub DeleteColumn(ByVal Name As String)
  39.         W.Columns.Remove(Name)
  40.         Save()
  41.     End Sub
  42.     Public Sub DeleteColumn(ByVal Index As Integer)
  43.         W.Columns.RemoveAt(Index)
  44.         Save()
  45.     End Sub
  46.     Public Sub RemoveItem(ByVal index As Integer)
  47.         W.Rows.RemoveAt(index)
  48.         Save()
  49.     End Sub
  50.     Public Function Columns() As String()
  51.         Dim q As New List(Of String)
  52.         For Each x As DataColumn In W.Columns
  53.             q.Add(x.ColumnName)
  54.         Next
  55.         Return q.ToArray
  56.     End Function
  57.     Public Function Columns(ByVal index As Integer) As String
  58.         Return W.Columns(index).ColumnName
  59.     End Function
  60.     Public Function Items(ByVal index As Integer) As dbItem
  61.         Return New dbItem(W.Rows(index), Me)
  62.     End Function
  63.  
  64.     Public Function FindItem(ByVal ColmIndex As Integer, ByVal Value As String) As dbItem()
  65.         Try
  66.             Dim q As New List(Of dbItem)
  67.             For Each x As Data.DataRow In _
  68.             W.Select(Columns(ColmIndex) & " Like '" & Value & "'")
  69.                 q.Add(New dbItem(x, Me))
  70.             Next
  71.             Return q.ToArray
  72.         Catch ex As Exception
  73.             Return New dbItem() {}
  74.         End Try
  75.     End Function
  76.     Public Sub ClearItems()
  77.         W.Rows.Clear()
  78.         Save()
  79.     End Sub
  80.     Public Function AddItem(ByVal ParamArray Values As String()) As dbItem
  81.         If FindItem(0, Values(0)).Length = 0 Then
  82.             Dim d = New dbItem(W.Rows.Add(Values), Me)
  83.             Save()
  84.             Return d
  85.         Else
  86.             Return FindItem(0, Values(0))(0)
  87.         End If
  88.     End Function
  89.     Public Function Count() As Integer
  90.         Return W.Rows.Count
  91.     End Function
  92.     Public Function Items() As dbItem()
  93.         Dim q As New List(Of dbItem)
  94.         SyncLock W.Rows
  95.             For Each x As Data.DataRow In W.Rows
  96.                 q.Add(New dbItem(x, Me))
  97.             Next
  98.         End SyncLock
  99.         Return q.ToArray
  100.     End Function
  101.     Public Class dbItem
  102.         Private db As njdb
  103.         Private rw As Data.DataRow
  104.         Sub New(ByVal rw As Data.DataRow, ByVal db As njdb)
  105.             Me.rw = rw
  106.             Me.db = db
  107.         End Sub
  108.         Public Property Value(ByVal index As Integer) As String
  109.             Get
  110.                 Return rw.Item(index)
  111.             End Get
  112.             Set(ByVal value As String)
  113.                 rw.Item(index) = value
  114.                 db.Save()
  115.             End Set
  116.         End Property
  117.         Public Sub Remove()
  118.             db.W.Rows.Remove(rw)
  119.             db.Save()
  120.         End Sub
  121.         Public Property Value(ByVal ColmName As String) As String
  122.             Get
  123.                 Return rw.Item(ColmName)
  124.             End Get
  125.             Set(ByVal value As String)
  126.                 rw.Item(ColmName) = value
  127.                 db.Save()
  128.             End Set
  129.         End Property
  130.         Public Property Values() As Object()
  131.             Get
  132.                 Return rw.ItemArray
  133.             End Get
  134.             Set(ByVal value As Object())
  135.                 rw.ItemArray = value
  136.                 db.Save()
  137.             End Set
  138.         End Property
  139.     End Class
  140. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement