io farei così
codice:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ValoreDiControllo As String = "Pietro"
Dim StringaConnessione As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\dati\test\test.mdb"
Dim result = ExecuteScalar(StringaConnessione, "select nome from tabella1 where id = 1")
If Not (Not (result Is Nothing OrElse result Is DBNull.Value) AndAlso result.ToString = ValoreDiControllo) Then
Application.Exit()
End If
'altro codice: test superato
Me.Label1.Text = "test superato"
End Sub
'-------------------------------------------------------------------------------------------------
'Esegue la query e restituisce la 1° colonna della 1° riga del recordset trovato
'Esegue la query e restituisce la prima colonna della prima riga nel gruppo
'di risultati restituito dalla query.
'Le colonne o le righe aggiuntive vengono ignorate
'
'restituisce Nothing se non trova valore
'restituisce System.DBNull.Value se trova valore null
'restituisce object se trova valore
'-------------------------------------------------------------------------------------------------
Public Function ExecuteScalar(StringaConnessione As String, stringaSQL As String) As Object
Dim Connessione As OleDbConnection = Nothing
Dim Comando As OleDbCommand
Try
Connessione = New OleDbConnection(StringaConnessione)
Connessione.Open()
Comando = New OleDbCommand(stringaSQL, Connessione)
Return Comando.ExecuteScalar()
Catch er As Exception
Throw
Finally
If Connessione IsNot Nothing Then Connessione.Close()
End Try
End Function