DataReader System.Data.OleDb






Imports System.Data.OleDb
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Codice generato da Progettazione Windows Form "

Public Sub New()
MyBase.New()

'Chiamata richiesta da Progettazione Windows Form.

InitializeComponent()

'Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent()


End Sub

'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Richiesto da Progettazione Windows Form

Private components As System.ComponentModel.IContainer

'NOTA: la procedura che segue e' richiesta da Progettazione Windows Form.

'Puo' essere modificata in Progettazione Windows Form.

'Non modificarla nell'editor del codice.

Friend WithEvents ApriChiudi As System.Windows.Forms.Button
Friend WithEvents Esci As System.Windows.Forms.Button
Friend WithEvents Trova As System.Windows.Forms.Button
Friend WithEvents Accesso As System.Windows.Forms.GroupBox
Friend WithEvents Ricerca As System.Windows.Forms.GroupBox
Friend WithEvents Visualizza As System.Windows.Forms.GroupBox
Friend WithEvents txtTrova As System.Windows.Forms.TextBox
Friend WithEvents txtOut As System.Windows.Forms.TextBox
Friend WithEvents Indietro As System.Windows.Forms.Button
Friend WithEvents Avanti As System.Windows.Forms.Button
Friend WithEvents Salva As System.Windows.Forms.Button
Friend WithEvents txtOut1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ApriChiudi = New System.Windows.Forms.Button()
Me.Esci = New System.Windows.Forms.Button()
Me.Trova = New System.Windows.Forms.Button()
Me.Accesso = New System.Windows.Forms.GroupBox()
Me.txtTrova = New System.Windows.Forms.TextBox()
Me.Ricerca = New System.Windows.Forms.GroupBox()
Me.Visualizza = New System.Windows.Forms.GroupBox()
Me.txtOut = New System.Windows.Forms.TextBox()
Me.Indietro = New System.Windows.Forms.Button()
Me.Avanti = New System.Windows.Forms.Button()
Me.Salva = New System.Windows.Forms.Button()
Me.txtOut1 = New System.Windows.Forms.TextBox()
Me.Ricerca.SuspendLayout()
Me.Visualizza.SuspendLayout()
Me.SuspendLayout()
'

'ApriChiudi

'

Me.ApriChiudi.Location = New System.Drawing.Point(32, 32)
Me.ApriChiudi.Name = "ApriChiudi"
Me.ApriChiudi.TabIndex = 0
Me.ApriChiudi.Text = "&Connetti"
'

'Esci

'

Me.Esci.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Esci.Location = New System.Drawing.Point(32, 304)
Me.Esci.Name = "Esci"
Me.Esci.TabIndex = 4
Me.Esci.Text = "&Esci"
'

'Trova

'

Me.Trova.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Trova.Location = New System.Drawing.Point(24, 56)
Me.Trova.Name = "Trova"
Me.Trova.TabIndex = 1
Me.Trova.Text = "&Trova"
'

'Accesso

'

Me.Accesso.Location = New System.Drawing.Point(8, 8)
Me.Accesso.Name = "Accesso"
Me.Accesso.Size = New System.Drawing.Size(128, 64)
Me.Accesso.TabIndex = 0
Me.Accesso.TabStop = False
Me.Accesso.Text = "Accesso"
'

'txtTrova

'

Me.txtTrova.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtTrova.Location = New System.Drawing.Point(24, 24)
Me.txtTrova.Name = "txtTrova"
Me.txtTrova.Size = New System.Drawing.Size(72, 20)
Me.txtTrova.TabIndex = 0
Me.txtTrova.Text = ""
'

'Ricerca

'

Me.Ricerca.Controls.AddRange(New System.Windows.Forms.Control() {Me.Trova, Me.txtTrova})
Me.Ricerca.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Ricerca.Location = New System.Drawing.Point(8, 80)
Me.Ricerca.Name = "Ricerca"
Me.Ricerca.Size = New System.Drawing.Size(128, 96)
Me.Ricerca.TabIndex = 1
Me.Ricerca.TabStop = False
Me.Ricerca.Text = "Inserisci l'indice"
'

'Visualizza

'

Me.Visualizza.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtOut1, Me.txtOut})
Me.Visualizza.Location = New System.Drawing.Point(8, 192)
Me.Visualizza.Name = "Visualizza"
Me.Visualizza.Size = New System.Drawing.Size(128, 56)
Me.Visualizza.TabIndex = 2
Me.Visualizza.TabStop = False
Me.Visualizza.Text = "Visualizza"
'

'txtOut

'

Me.txtOut.Location = New System.Drawing.Point(8, 24)
Me.txtOut.Multiline = True
Me.txtOut.Name = "txtOut"
Me.txtOut.Size = New System.Drawing.Size(24, 24)
Me.txtOut.TabIndex = 0
Me.txtOut.Text = ""
'

'Indietro

'

Me.Indietro.Location = New System.Drawing.Point(8, 264)
Me.Indietro.Name = "Indietro"
Me.Indietro.Size = New System.Drawing.Size(32, 23)
Me.Indietro.TabIndex = 5
Me.Indietro.Text = "<"
'

'Avanti

'

Me.Avanti.Location = New System.Drawing.Point(40, 264)
Me.Avanti.Name = "Avanti"
Me.Avanti.Size = New System.Drawing.Size(32, 23)
Me.Avanti.TabIndex = 6
Me.Avanti.Text = ">"
'

'Salva

'

Me.Salva.Location = New System.Drawing.Point(80, 264)
Me.Salva.Name = "Salva"
Me.Salva.Size = New System.Drawing.Size(56, 23)
Me.Salva.TabIndex = 7
Me.Salva.Text = "Salva"
'

'txtOut1

'

Me.txtOut1.Location = New System.Drawing.Point(32, 24)
Me.txtOut1.Multiline = True
Me.txtOut1.Name = "txtOut1"
Me.txtOut1.Size = New System.Drawing.Size(88, 24)
Me.txtOut1.TabIndex = 1
Me.txtOut1.Text = ""
'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.Esci
Me.ClientSize = New System.Drawing.Size(144, 341)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Salva, Me.Avanti, Me.Indietro, Me.Visualizza, Me.ApriChiudi, Me.Accesso, Me.Ricerca, Me.Esci})
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.Ricerca.ResumeLayout(False)
Me.Visualizza.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=G:\Archivio\_Nasi\Biblio.mdb;" 'Dichiaro la stringa di connessione
Dim cn As New OleDbConnection(ConnString) 'Dichiaro la nuova connessione
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Sub
Private Sub ApriChiudi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApriChiudi.Click
If ApriChiudi.Text = "&Connetti" Then
cn.Open()
ApriChiudi.Text = "&Disconnetti"
Else
ApriChiudi.Text = "&Connetti"
cn.Close()
End If

If (cn.State) = ConnectionState.Open Then
MessageBox.Show("Il db adesso e' connesso", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Il db adesso e' disconnesso", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub Esci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Esci.Click
cn.Close()
End
End Sub
Private Sub Trova_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trova.Click
Dim testotxt As String = txtTrova.Text
txtOut.Text = ""
txtTrova.Focus()

If cn.State = ConnectionState.Open Then

Dim sql As String = "SELECT DISTINCT * FROM authors WHERE au_id >= ?"
Dim cmd As New OleDbCommand(sql, cn)
cmd.Parameters.Add("au_id", testotxt)
Dim dr As OleDbDataReader = cmd.ExecuteReader
dr.Read()
txtOut.Text = (dr(0))
txtOut1.Text = (dr(1))

If txtOut.Text = "" Then
MessageBox.Show("Nessun autore corrisponde a questo indice", "Attenzione!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If

dr.Close()
Else
MessageBox.Show("Il db non e' ancora connesso", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If

End Sub
Private Sub Indietro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Indietro.Click
Dim sql As String = "SELECT TOP 1 * FROM authors WHERE au_id < (" & txtOut.Text & ") ORDER BY au_id DESC"
Dim cmd As New OleDbCommand(sql, cn)
Dim dr As OleDbDataReader = cmd.ExecuteReader
dr.Read()
txtOut.Text = dr(0)
txtOut1.Text = dr(1)
dr.Close()
End Sub
Private Sub Avanti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Avanti.Click
Dim sql As String = "SELECT TOP 1 * FROM authors WHERE au_id > (" & txtOut.Text & ") ORDER BY au_id "
Dim cmd As New OleDbCommand(sql, cn)
Dim dr As OleDbDataReader = cmd.ExecuteReader
dr.Read()
txtOut.Text = dr(0)
txtOut1.Text = dr(1)
dr.Close()
End Sub
Private Sub Salva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Salva.Click
Dim sql As String = "UPDATE authors SET author= '" & txtOut1.Text & "' WHERE Au_id = " & txtOut.Text
Dim cmd As New OleDbCommand(sql, cn)
Dim dr As OleDbDataReader
cmd.ExecuteNonQuery()
dr.Close()
End Sub
End Class










( datareadersystemdataoledb.html )- by Paolo Puglisi - Modifica del 17/12/2023