Esempio dei metodi Save e Open (VB)

Questi tre esempi illustrano come è possibile usare insieme i metodi Save e Open.

Si supponga di dover partire per un viaggio d'affari e di voler seguire una tabella di un database. Prima di andare, accedere ai dati come oggetto Recordset e salvarli in un formato trasportabile. Quando si arriva a destinazione, si accede a Recordset come oggetto Recordset locale e disconnesso. Salvare di nuovo l'oggetto Recordset dopo aver apportato le modifiche. Infine, quando si torna a casa, ci si connette nuovamente al database e lo si aggiorna con le modifiche apportate in viaggio.

Prima di tutto, accedere e salvare la tabella Authors.

'BeginSaveVB  
  
    'To integrate this code  
    'replace the data source and initial catalog values  
    'in the connection string  
  
Public Sub Main()  
    On Error GoTo ErrorHandler  
  
    'recordset and connection variables  
    Dim rstAuthors As ADODB.Recordset  
    Dim Cnxn As ADODB.Connection  
    Dim strCnxn As String  
    Dim strSQLAuthors As String  
  
    ' Open connection  
    Set Cnxn = New ADODB.Connection  
    strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _  
        "Initial Catalog='Pubs';Integrated Security='SSPI';"  
    Cnxn.Open strCnxn  
  
    Set rstAuthors = New ADODB.Recordset  
    strSQLAuthors = "SELECT au_id, au_lname, au_fname, city, phone FROM Authors"  
    rstAuthors.Open strSQLAuthors, Cnxn, adOpenDynamic, adLockOptimistic, adCmdText  
  
    'For sake of illustration, save the Recordset to a diskette in XML format  
    rstAuthors.Save "c:\Pubs.xml", adPersistXML  
  
    ' clean up  
    rstAuthors.Close  
    Cnxn.Close  
    Set rstAuthors = Nothing  
    Set Cnxn = Nothing  
    Exit Sub  
  
ErrorHandler:  
    'clean up  
    If Not rstAuthors Is Nothing Then  
        If rstAuthors.State = adStateOpen Then rstAuthors.Close  
    End If  
    Set rstAuthors = Nothing  
  
    If Not Cnxn Is Nothing Then  
        If Cnxn.State = adStateOpen Then Cnxn.Close  
    End If  
    Set Cnxn = Nothing  
  
    If Err <> 0 Then  
        MsgBox Err.Source & "-->" & Err.Description, , "Error"  
    End If  
End Sub  
'EndSaveVB  

A questo punto, si è arrivato a destinazione. Si accederà alla tabella Authors come oggetto Recordset locale e disconnesso. Il provider MSPersist deve essere presente nel computer usato per accedere al file salvato a:\Pubs.xml.

Attribute VB_Name = "Save"  

A questo punto, è possibile tornare alla home page. Aggiornare il database con le modifiche apportate.

Attribute VB_Name = "Save"  

Vedere anche

Metodo Open (Recordset - ADO)
Oggetto Recordset (ADO)
Altre informazioni sulla persistenza dei recordset
Metodo Save