Optimise SQLite connect in only one funtion

This commit is contained in:
JackCarterSmith 2017-05-29 16:40:27 +02:00
parent 5b82515c0b
commit 1fcdd9c482
7 changed files with 96 additions and 45 deletions

View File

@ -21,7 +21,6 @@
Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
Me.Close() Me.Close()
Main.Show()
End Sub End Sub
End Class End Class

View File

@ -24,13 +24,16 @@ Partial Class AddMachine
Private Sub InitializeComponent() Private Sub InitializeComponent()
Me.SuspendLayout() Me.SuspendLayout()
' '
'AddMachine_2 'AddMachine
' '
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 261) Me.ClientSize = New System.Drawing.Size(284, 261)
Me.Name = "AddMachine_2" Me.MaximizeBox = False
Me.Text = "AddMachine_2" Me.MinimizeBox = False
Me.Name = "AddMachine"
Me.ShowIcon = False
Me.Text = "Ajouter un ordinateur"
Me.ResumeLayout(False) Me.ResumeLayout(False)
End Sub End Sub

View File

@ -1,5 +1,10 @@
Public Class AddMachine Public Class AddMachine
Public Shared newID As Integer
Private Sub AddMachine_2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Private Sub AddMachine_2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim newID As Integer Dim addMachine_IdForm As AddMachine_ID
addMachine_IdForm = New AddMachine_ID()
addMachine_IdForm.Show()
addMachine_IdForm = Nothing
End Sub End Sub
End Class End Class

View File

@ -23,9 +23,11 @@ Partial Class AddMachine_ID
<System.Diagnostics.DebuggerStepThrough()> _ <System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent() Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label() Me.Label1 = New System.Windows.Forms.Label()
Me.MaskedTextBox1 = New System.Windows.Forms.MaskedTextBox() Me.newIDBox = New System.Windows.Forms.MaskedTextBox()
Me.CancelButton = New System.Windows.Forms.Button() Me.AddMachine_ID_CancelButton = New System.Windows.Forms.Button()
Me.NextButton = New System.Windows.Forms.Button() Me.NextButton = New System.Windows.Forms.Button()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout() Me.SuspendLayout()
' '
'Label1 'Label1
@ -39,28 +41,28 @@ Partial Class AddMachine_ID
Me.Label1.Text = "Scanner un ID" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "ou entrez le manuellement" Me.Label1.Text = "Scanner un ID" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "ou entrez le manuellement"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
' '
'MaskedTextBox1 'newIDBox
' '
Me.MaskedTextBox1.AllowDrop = True Me.newIDBox.AllowDrop = True
Me.MaskedTextBox1.AsciiOnly = True Me.newIDBox.AsciiOnly = True
Me.MaskedTextBox1.BeepOnError = True Me.newIDBox.BeepOnError = True
Me.MaskedTextBox1.Location = New System.Drawing.Point(134, 92) Me.newIDBox.Location = New System.Drawing.Point(6, 16)
Me.MaskedTextBox1.Mask = "9999999999" Me.newIDBox.Mask = "9999999999"
Me.MaskedTextBox1.Name = "MaskedTextBox1" Me.newIDBox.Name = "newIDBox"
Me.MaskedTextBox1.PromptChar = Global.Microsoft.VisualBasic.ChrW(88) Me.newIDBox.PromptChar = Global.Microsoft.VisualBasic.ChrW(88)
Me.MaskedTextBox1.Size = New System.Drawing.Size(100, 20) Me.newIDBox.Size = New System.Drawing.Size(100, 20)
Me.MaskedTextBox1.TabIndex = 0 Me.newIDBox.TabIndex = 0
Me.MaskedTextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center Me.newIDBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
' '
'CancelButton 'AddMachine_ID_CancelButton
' '
Me.CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.AddMachine_ID_CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.CancelButton.Location = New System.Drawing.Point(146, 132) Me.AddMachine_ID_CancelButton.Location = New System.Drawing.Point(146, 132)
Me.CancelButton.Name = "CancelButton" Me.AddMachine_ID_CancelButton.Name = "AddMachine_ID_CancelButton"
Me.CancelButton.Size = New System.Drawing.Size(75, 23) Me.AddMachine_ID_CancelButton.Size = New System.Drawing.Size(75, 23)
Me.CancelButton.TabIndex = 2 Me.AddMachine_ID_CancelButton.TabIndex = 2
Me.CancelButton.Text = "Annuler" Me.AddMachine_ID_CancelButton.Text = "Annuler"
Me.CancelButton.UseVisualStyleBackColor = True Me.AddMachine_ID_CancelButton.UseVisualStyleBackColor = True
' '
'NextButton 'NextButton
' '
@ -71,29 +73,42 @@ Partial Class AddMachine_ID
Me.NextButton.Text = "Suivant" Me.NextButton.Text = "Suivant"
Me.NextButton.UseVisualStyleBackColor = True Me.NextButton.UseVisualStyleBackColor = True
' '
'AddMachine 'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.newIDBox)
Me.GroupBox1.Location = New System.Drawing.Point(129, 74)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(112, 42)
Me.GroupBox1.TabIndex = 101
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Nouvel ID"
'
'AddMachine_ID
' '
Me.AcceptButton = Me.NextButton Me.AcceptButton = Me.NextButton
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.CausesValidation = False Me.CausesValidation = False
Me.ClientSize = New System.Drawing.Size(360, 167) Me.ClientSize = New System.Drawing.Size(360, 167)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.NextButton) Me.Controls.Add(Me.NextButton)
Me.Controls.Add(Me.CancelButton) Me.Controls.Add(Me.AddMachine_ID_CancelButton)
Me.Controls.Add(Me.MaskedTextBox1)
Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Label1)
Me.MaximizeBox = False Me.MaximizeBox = False
Me.MinimizeBox = False Me.MinimizeBox = False
Me.Name = "AddMachine" Me.Name = "AddMachine_ID"
Me.ShowIcon = False Me.ShowIcon = False
Me.Text = "Ajouter un ordinateur" Me.Text = "Ajouter un ordinateur"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.ResumeLayout(False) Me.ResumeLayout(False)
Me.PerformLayout() Me.PerformLayout()
End Sub End Sub
Friend WithEvents Label1 As Label Friend WithEvents Label1 As Label
Friend WithEvents MaskedTextBox1 As MaskedTextBox Friend WithEvents newIDBox As MaskedTextBox
Friend WithEvents CancelButton As Button Friend WithEvents AddMachine_ID_CancelButton As Button
Friend WithEvents NextButton As Button Friend WithEvents NextButton As Button
Friend WithEvents GroupBox1 As GroupBox
End Class End Class

View File

@ -3,11 +3,20 @@
End Sub End Sub
Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles AddMachine_ID_CancelButton.Click
Me.Close() Me.Close()
End Sub End Sub
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click Private Sub CheckIDAlreadyUsed(idToCheck As String)
Main.RequestToSQLite("")
End Sub
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
If newIDBox.ValidateText And newIDBox.Text.Length() = 10 Then
AddMachine.newID = newIDBox.Text
Else
MsgBox("Format d'ID incorrect, entrer uniquement 10 chiffres.", 48, "Entrée incorrect")
End If
End Sub End Sub
End Class End Class

View File

@ -469,11 +469,12 @@ Partial Class Main
' '
'IDBox 'IDBox
' '
Me.IDBox.InsertKeyMode = System.Windows.Forms.InsertKeyMode.Overwrite
Me.IDBox.Location = New System.Drawing.Point(37, 16) Me.IDBox.Location = New System.Drawing.Point(37, 16)
Me.IDBox.Mask = "9999999999" Me.IDBox.Mask = "9999999999"
Me.IDBox.Name = "IDBox" Me.IDBox.Name = "IDBox"
Me.IDBox.Size = New System.Drawing.Size(79, 20) Me.IDBox.Size = New System.Drawing.Size(79, 20)
Me.IDBox.TabIndex = 17 Me.IDBox.TabIndex = 0
Me.IDBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center Me.IDBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
' '
'Main 'Main

View File

@ -1,8 +1,5 @@
Imports System.ComponentModel Imports System.Data.SQLite
Imports System.Data.SQLite
Public Class Main Public Class Main
Private idSelected As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListAllInv() ListAllInv()
@ -18,6 +15,33 @@ Public Class Main
If HWCheck.Checked Then OSCheck.Enabled = True Else OSCheck.Enabled = False If HWCheck.Checked Then OSCheck.Enabled = True Else OSCheck.Enabled = False
End Sub End Sub
Public Function RequestToSQLite(SQLiteCommand As String) As DataTable
Dim dt As DataTable = Nothing
Dim ds As New DataSet
Try
StatusLabel.Text = "Récupération des données depuis la base SQLite..."
Using con As New SQLiteConnection("Data Source=db.sqlite")
Using cmd As New SQLiteCommand(SQLiteCommand, con)
con.Open()
Using da As New SQLiteDataAdapter(cmd)
da.Fill(ds)
dt = ds.Tables(0)
End Using
End Using
End Using
Return dt
StatusLabel.Text = "Récupération avec succés de la base SQLite."
Catch ex As Exception
StatusLabel.Text = "Une erreur avec la base SQLite s'est produite !"
MsgBox(ex.Message)
End Try
Return Nothing
End Function
Private Sub ListAllInv() Private Sub ListAllInv()
StatusLabel.Text = "Récupération des données depuis la base SQLite..." StatusLabel.Text = "Récupération des données depuis la base SQLite..."
Dim connectStr As String = "Data Source=db.sqlite" Dim connectStr As String = "Data Source=db.sqlite"
@ -89,7 +113,6 @@ Public Class Main
End Sub End Sub
Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
Me.Hide()
Dim aboutForm As About Dim aboutForm As About
aboutForm = New About() aboutForm = New About()
aboutForm.Show() aboutForm.Show()
@ -101,13 +124,9 @@ Public Class Main
addMachineForm = New AddMachine() addMachineForm = New AddMachine()
addMachineForm.Show() addMachineForm.Show()
addMachineForm = Nothing addMachineForm = Nothing
Dim addMachine_IdForm As AddMachine_ID
addMachine_IdForm = New AddMachine_ID()
addMachine_IdForm.Show()
addMachine_IdForm = Nothing
End Sub End Sub
Private Sub IDBox_Click(sender As Object, e As EventArgs) Private Sub IDBox_Click(sender As Object, e As EventArgs)
If idSelected Then IDBox.Text = "" IDBox.SelectAll()
End Sub End Sub
End Class End Class