PRB: ADOCE Records May Be Lost When Power Turned Off (308476)



The information in this article applies to:

  • Microsoft eMbedded Visual Basic 3.0

This article was previously published under Q308476

SYMPTOMS

Microsoft CE Data Access 3.1 (ADOCE) records that are stored in a persistent database may be lost when the device is suspended, reset, or shut down, even after the database connection has been closed.

MORE INFORMATION

Records are flushed to the database only when the connection object is closed and released.

If you are using recordsets directly to write data into a database without connecting the recordset to the connection object, a temporary connection object will be created and destroyed internally. In this case, the connection string is passed as the second parameter in the Open method of the recordset. The last record will be saved only when the internal record cursor moves from the current location.

In your application, if you already have a connection object that is connected to a database but you are using recordsets directly to write data without explicitly connecting the recordset to the connection object, then the existing connection object will be used and data will be saved only after the connection object has been closed and released.

Steps to Reproduce Behavior

  1. Start a new Windows CE project in eMbedded Visual Basic.
  2. From the Project menu, click Components, and then select Microsoft CE ListView Control 6.0.
  3. From the Project menu, click References, and add a reference to the Microsoft CE ADO Control 3.1.
  4. Add four command buttons and one listview control to Form1. Do not be concerned with the placement of the controls.
  5. Paste the following code into Form1:
    Option Explicit
    Dim cnn As ADOCE.Connection
    Dim rs As ADOCE.Recordset
    
    Private Sub Form_Load()
        Command1.Move 120, 120, 3255, 255
        Command2.Move 120, 480, 3255, 255
        Command3.Move 120, 840, 3255, 255
        Command4.Move 120, 1200, 3255, 255
        ListViewCtrl1.Move 120, 1560, 3255, 1695
        Command1.Caption = "Create Database"
        Command2.Caption = "Add Record"
        Command3.Caption = "Release Connection Object"
        Command4.Caption = "View Data"
        Create_DB
    End Sub
    
    Private Sub Command1_Click()
        Create_DB
    End Sub
    
    Private Sub Command2_Click()
        Set cnn = CreateObject("ADOCE.Connection.3.1")
        Set rs = CreateObject("ADOCE.Recordset.3.1")
            
        cnn.Open "Provider=CEDB;Data Source = \My Documents\db1.cdb"
        rs.Open "Table1", cnn, 1, 3
        
        rs.AddNew
        rs.Fields("Field1") = "Edgar"
        rs.Fields("Field2") = "Martinez"
        rs.Update
        
        rs.Close
        cnn.Close
    End Sub
    
    Private Sub Command3_Click()
        Set cnn = Nothing
    End Sub
    
    Private Sub Command4_Click()
        Open_RS
    End Sub
    
    Private Sub Form_OKClick()
        App.End
    End Sub
    
    Private Sub Open_RS()
        ListViewCtrl1.ListItems.Clear
    
        Set rs = CreateObject("adoce.recordset.3.1")
        
        rs.Open "Table1", "\My Documents\db1.cdb", 1, 3, 2
        
        'fill listview
        Dim clmX As ColumnHeader
        Dim itmX As ListItem
        
        ListViewCtrl1.View = lvwReport 'use reportview no icons
        Set clmX = ListViewCtrl1.ColumnHeaders.Add(, , "Field1", _
            ListViewCtrl1.Width / 2)
        Set clmX = ListViewCtrl1.ColumnHeaders.Add(, , "Field2", _
            ListViewCtrl1.Width / 2)
        
        While Not rs.EOF
            Set itmX = ListViewCtrl1.ListItems.Add(, , rs.Fields("Field1"))
            itmX.SubItems(1) = rs.Fields("Field2")
            rs.MoveNext
        Wend
    End Sub
    
    Private Sub Create_DB()
        Dim sSQL As String
        
        Set rs = CreateObject("ADOCE.Recordset.3.1")
        
        On Error Resume Next
    
        sSQL = "CREATE DATABASE '\My Documents\db1.cdb'"
        rs.Open sSQL
        rs.Close
        Set rs = Nothing
        
        Set rs = CreateObject("ADOCE.Recordset.3.1")
        sSQL = "CREATE TABLE Table1 (Field1 Text, Field2 Text)"
        rs.Open sSQL, "\My Documents\db1.cdb", 1, 1
        rs.Close
        
        Set rs = Nothing
    End Sub
    					
  6. Run the project and click the Create Database command button.
  7. Click the Add Record command button.
  8. Click the Release Connection Object command button.
  9. Click the View Data command button and note that the record was added.
  10. Click Add Record again.
  11. Click View Data and note that the record has been added.
  12. Perform a soft ("warm") reset of the device and run the eMbedded Visual Basic application again.
  13. Click View Data and note that the record was not persisted because the connection object was never set to "nothing".

REFERENCES

For additional information, visit the following DEVBUZZ.COM Web site:

Forcing Database Changes from ADOCE to Commit
http://www.deVBuzz.com/content/zinc_db_force_commit_pg1.asp

Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

Modification Type:MinorLast Reviewed:7/27/2004
Keywords:kbprb KB308476