Save 및 Open 메서드 예제(VB)

다음 세 가지 예제에서는 SaveOpen 메서드를 함께 사용하는 방법을 보여 줍니다.

출장 중이며 데이터베이스에서 테이블을 사용하려는 경우를 가정합니다. 출발가기 전에 레코드 집합으로 데이터에 액세스하고 전송 가능한 형식으로 저장합니다. 대상에 도착하면 연결이 끊긴 로컬 레코드 집합으로 레코드 집합에 액세스합니다. 레코드 집합을 변경한 다음, 다시 저장합니다. 마지막으로, 집으로 돌아오면 데이터베이스에 다시 연결하여 이동 중에 변경한 내용으로 업데이트합니다.

먼저 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  

이 시점에서 목적지에 도착했습니다. 연결이 끊긴 로컬 레코드 집합으로 Authors 테이블에 액세스합니다. 저장된 파일(a:\Pubs.xml)에 액세스하는 데 사용하는 컴퓨터에 MSPersist 공급자가 있어야 합니다.

Attribute VB_Name = "Save"  

마지막으로, 집으로 돌아갑니다. 변경 내용으로 데이터베이스를 업데이트합니다.

Attribute VB_Name = "Save"  

참고 항목

Open 메서드(ADO 레코드 집합)
레코드 집합 개체(ADO)
레코드 집합 지속성에 대한 자세한 정보
Save 메서드