'INCLUDERE I SEGUENTI RIFERIEMTNI PER L'ACCESSO AI DATABASE:
'- Microsft ActiveX Data Objects 2.5 Library
'- Microsft ActiveX Data Objects Recorset 2.8 Library

'DICHIARARE DUE VARIABILE (RECORDSET E CONNECTION)
'- Public CONN As New ADODB.Connection
'- Public RS As New ADODB.Recordset
'CONN E RS Sono dei nomi di variabili di esempio e possono essere variati

Imports System.Data.OleDb

Module Database
    Public CONN As OleDbConnection
    Public RS As OleDbCommand

    Dim stringasql As String
    Dim stringaconn As String



    '*************** C O N N E S S I O N E *****************

    Public Sub Conn_Crea(ByRef Connessione As OleDbConnection)
        Connessione = New OleDbConnection
    End Sub

    Public Sub Conn_Percorso(ByRef Connessione As OleDbConnection, ByVal Percorso As String)
        Connessione.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & Percorso & "';"
    End Sub

    Public Sub Conn_Apri(ByVal Connessione As OleDbConnection)
        Connessione.Open()
    End Sub


    Public Sub Conn_Chiudi(ByVal Connessione As OleDbConnection)
        Connessione.Close()
    End Sub

    Public Sub Conn_Distruggi(ByVal Connessione As OleDbConnection)
        Connessione = Nothing
    End Sub


    Public Sub Conn_CreaApri(ByVal Percorso As String)
        Conn_Crea(CONN)
        Conn_Percorso(CONN, Percorso)
        Conn_Apri(CONN)
    End Sub


    Public Sub Conn_ChiudiDistruggi()
        Conn_Chiudi(CONN)
        Conn_Distruggi(CONN)
    End Sub


    ''****************** R E C O R D S E T *******************

    Public Sub RS_Crea(ByRef Recordset As OleDbCommand)
        Recordset = New OleDbCommand
    End Sub


    Public Function RS_Cerca(ByVal Recordset As OleDbCommand, ByVal Connessione As OleDbConnection, ByVal STSQL As String, ByRef Nome_Letto As String) As Integer
        Dim quanti As Integer = 0
        ' Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(STSQL, Connessione)

        Connessione.Open()
        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Nome_Letto = reader.GetString(1)
            quanti += 1
            'MsgBox(reader.GetInt32(0).ToString() + ", " + reader.GetString(1) + "," + reader.GetString(2), MsgBoxStyle.OkOnly, "Dato DB")
        End While
        ' always call Close when done reading.
        reader.Close()

        Return quanti
    End Function

    Public Function RS_Inserisci(ByVal Recordset As OleDbCommand, ByVal Connessione As OleDbConnection, ByVal STSQL As String) As Boolean
        On Error GoTo errore
        Dim nome As String = ""
        Dim command As New OleDbCommand(STSQL, Connessione)
        Connessione.Open()
        command.ExecuteNonQuery()
        Return False
errore:
        Return True
    End Function

    Public Function RS_Aggiorna(ByVal Recordset As OleDbCommand, ByVal Connessione As OleDbConnection, ByVal STSQL As String) As Boolean
        On Error GoTo errore
        Dim nome As String = ""
        Dim command As New OleDbCommand(STSQL, Connessione)
        Connessione.Open()
        command.ExecuteNonQuery()
        Return False
errore:
        Return True
    End Function

    Public Function RS_Cancella(ByVal Recordset As OleDbCommand, ByVal Connessione As OleDbConnection, ByVal STSQL As String) As Boolean
        On Error GoTo errore
        Dim nome As String = ""
        Dim command As New OleDbCommand(STSQL, Connessione)
        Connessione.Open()
        command.ExecuteNonQuery()
        Return False
errore:
        Return True
    End Function
End Module
