4dcaf7a42a
- rese più sicure alcune chiamate a Sqlite (e con più log).
133 lines
6.5 KiB
VB.net
133 lines
6.5 KiB
VB.net
Imports System.IO
|
|
Imports System.Data.SQLite
|
|
Imports EgtUILib
|
|
|
|
Module ManageDb
|
|
|
|
#Region "FIELDS & PROPERTIES"
|
|
|
|
Public Const DB_FILENAME As String = "OmagPHOTODb.sqlite"
|
|
|
|
Private m_DbConnection As SQLiteConnection
|
|
Public ReadOnly Property DbConnection As SQLiteConnection
|
|
Get
|
|
Return m_DbConnection
|
|
End Get
|
|
End Property
|
|
|
|
#End Region ' FIELDS & PROPERTIES
|
|
|
|
#Region "METHODS"
|
|
|
|
Friend Sub CreateDbFile()
|
|
SQLiteConnection.CreateFile(Map.refMainWindowVM.MainWindowM.sPhotoDir & "\" & DB_FILENAME)
|
|
End Sub
|
|
|
|
Friend Function ConnectToDb() As Boolean
|
|
Dim DbPath As String = Map.refMainWindowVM.MainWindowM.sPhotoDir & "\" & DB_FILENAME
|
|
If Not File.Exists(DbPath) Then Return False
|
|
m_DbConnection = New SQLiteConnection("Data Source = " & DbPath & "; Version = 3;")
|
|
Return Not IsNothing(m_DbConnection)
|
|
End Function
|
|
|
|
Friend Function ConnectToDb(DbPath As String) As Boolean
|
|
EgtOutLog("Connecting to DB " & DbPath)
|
|
If Not File.Exists(DbPath) Then Return False
|
|
Try
|
|
m_DbConnection = New SQLiteConnection("Data Source = " & DbPath & "; Version = 3;")
|
|
' Verifica integrità del DB
|
|
m_DbConnection.Open()
|
|
Dim Command As SQLiteCommand = New SQLiteCommand("PRAGMA quick_check;", m_DbConnection)
|
|
Dim Reader As SQLiteDataReader = Command.ExecuteReader()
|
|
Reader.Read()
|
|
Dim sRes As String = Reader(0)
|
|
m_DbConnection.Close()
|
|
Catch ex As Exception
|
|
EgtOutLog(ex.Message)
|
|
Return False
|
|
End Try
|
|
Return Not IsNothing(m_DbConnection)
|
|
End Function
|
|
|
|
Friend Sub CreateTable()
|
|
Dim SqlCommand As String = "CREATE TABLE " & Slab.DB_SLABS & " (" & Slab.DB_ID & " TEXT PRIMARY KEY," &
|
|
Slab.DB_IMAGEPATH & " TEXT," &
|
|
Slab.DB_STATE & " INTEGER," &
|
|
Slab.DB_PROJASSIGNEDTO & " TEXT," &
|
|
Slab.DB_MATERIAL & " TEXT," &
|
|
Slab.DB_THICKNESS & " REAL," &
|
|
Slab.DB_WAREHOUSEPOS & " TEXT," &
|
|
Slab.DB_ADDEDDATE & " INTEGER)"
|
|
ExecuteQuery(SqlCommand)
|
|
End Sub
|
|
|
|
Friend Function ExecuteQuery(SqlQuery As String) As Integer
|
|
m_DbConnection.Open()
|
|
Dim Command As SQLiteCommand = New SQLiteCommand(SqlQuery, m_DbConnection)
|
|
Dim ModifiedRowNumber As Integer = Command.ExecuteNonQuery()
|
|
m_DbConnection.Close()
|
|
Return ModifiedRowNumber
|
|
End Function
|
|
|
|
Friend Function ExecuteReaderQuery(SqlQuery As String) As SQLiteDataReader
|
|
Dim Command As SQLiteCommand = New SQLiteCommand(SqlQuery, m_DbConnection)
|
|
Dim Reader As SQLiteDataReader = Command.ExecuteReader()
|
|
Return Reader
|
|
End Function
|
|
|
|
Friend Function ExecuteSlabReaderQuery(SqlQuery As String) As List(Of Slab)
|
|
Dim SlabList As New List(Of Slab)
|
|
ManageDb.DbConnection.Open()
|
|
Try
|
|
Dim Command As SQLiteCommand = New SQLiteCommand(SqlQuery, m_DbConnection)
|
|
Dim Reader As SQLiteDataReader = Command.ExecuteReader()
|
|
While Reader.Read()
|
|
SlabList.Add(New Slab(Reader))
|
|
End While
|
|
Catch ex As Exception
|
|
End Try
|
|
ManageDb.DbConnection.Close()
|
|
Return SlabList
|
|
End Function
|
|
|
|
Friend Sub AddRandomRows(nRow As Integer)
|
|
Dim ImagePathList() As String = {"c:\EgtData\OmagPHOTO\Data\Lastra15_18.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra18_10.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra313.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra313_b.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra3138bit.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra33-18_234.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra55_33.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Lastra55-23.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Prova1.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Prova2.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Prova3.jpg",
|
|
"c:\EgtData\OmagPHOTO\Data\Prova4.jpg"}
|
|
Dim Text As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
For i = 0 To nRow
|
|
Dim R As New Random()
|
|
Dim Name As String = "Lastra" & i
|
|
Dim ImagePath As String = ImagePathList(R.Next(0, ImagePathList.Length))
|
|
Dim SlabState As String = Map.refOptionPanelVM.StateList(R.Next(0, Map.refOptionPanelVM.StateList.Count)).Id.ToString
|
|
Dim Material As String = Map.refOptionPanelVM.MaterialList(R.Next(0, Map.refOptionPanelVM.MaterialList.Count))
|
|
Dim SlabThickness As String = (R.Next(200, 700) / 10).ToString
|
|
Dim WarehousePosition As String = Text(R.Next(Text.Length)) & R.Next(1, 100) & "." & R.Next(1, 100)
|
|
Dim AddedDate As DateTime = DateTime.Now - New TimeSpan(R.Next(0, 730), R.Next(0, 24), R.Next(0, 60), R.Next(0, 60))
|
|
' Aggiungo la nuova lastra al Db
|
|
Dim Query As String = "INSERT INTO " & Slab.DB_SLABS & " (" & Slab.DB_ID & ", " & Slab.DB_IMAGEPATH & ", " & Slab.DB_STATE & ", " & Slab.DB_PROJASSIGNEDTO & ", " & Slab.DB_MATERIAL & ", " & Slab.DB_THICKNESS & ", " & Slab.DB_WAREHOUSEPOS & ", " & Slab.DB_ADDEDDATE & ")" &
|
|
" VALUES ('" & Name & "', " &
|
|
"'" & ImagePath & "', " &
|
|
SlabState & ", " &
|
|
"'" & "C:\EgtData\OmagOFFICE\Progetto" & i & "', " &
|
|
"'" & Material & "', " &
|
|
SlabThickness & ", " &
|
|
"'" & WarehousePosition & "', " &
|
|
"Date('" & (String.Format("{0:yyyy-MM-dd}", AddedDate)) & "'))"
|
|
ManageDb.ExecuteQuery(Query)
|
|
Next
|
|
End Sub
|
|
|
|
#End Region ' METHODS
|
|
|
|
End Module
|