Ouverture propre d'une connexion SQL
Bonjour,
Je n'ai jamais traité de SQL en VB, et je me pose quelques questions de structure.
Pouvez vous me dire quelle structure ci dessous est la plus "propre"?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Public Class Form 1
Public DBConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.sourcebdd & ";Jet OLEDB:Database Password=" & My.Settings.mdpbase & ";")
Public Command As OleDbCommand = DBConnection.CreateCommand()
Private Sub sub1()
DBConnection.Open()
Command.CommandText = "..."
...
DBConnection.Close()
Private Sub sub2()
DBConnection.Open()
Command.CommandText = "..."
...
DBConnection.Close()
End Sub
Sub
End Class |
ou
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Public Class Form 1
Private Sub sub1()
Dim DBConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.sourcebdd & ";Jet OLEDB:Database Password=" & My.Settings.mdpbase & ";")
Dim Command As OleDbCommand = DBConnection.CreateCommand()
DBConnection.Open()
Command.CommandText = "..."
...
DBConnection.Close()
Private Sub sub2()
Dim DBConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.sourcebdd & ";Jet OLEDB:Database Password=" & My.Settings.mdpbase & ";")
Dim Command As OleDbCommand = DBConnection.CreateCommand()
DBConnection.Open()
Command.CommandText = "..."
...
DBConnection.Close()
End Sub
Sub
End Class |
Ou y a-t-il mieux? Dans la déclaration DIm / Public / Pivate ?
Mon but étant de ne pas ouvrir trop de connections quand ça pourrait être inutile; alléger le code. Qu'à l'execution il ne se perde pas... enfin ce genre de trucs :)
Merci