Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CDaoRecordSet and CByteArray HELP!

Posted on 1998-05-11
4
Medium Priority
?
423 Views
Last Modified: 2013-11-20
VCPP 5.0 Win95
I am trying to store and retrieve a CByteArray to my DaoRecordset and not succeeding. To write the data I am doing the following:
BYTE abyteTemp[MAX];
CByteArray array ;
CDaoRecordset rs (&m_db);
char szQuery[256];
sprintf(szQuery,"SELECT * FROM MyTable");
rs.Open(dbOpenDynaset,szQuery);
rs.SetBookmark(rs.GetLastModifiedBookmark());
rs.AddNew();

array.SetSize( m_nSizeOfData ) ;
for (INT n=0;n<m_nSizeOfData;++n)
array.Add(abyteTemp[n]) ;

COleVariant v(array) ;
rs.SetFieldValue("Data0", v);
rs.Update();

After I execute this code, I can open my database table with Access and see that there is data written to the proper dbLongBinary field.  The field reads. “Long binary data”.  I am not sure if the proper data has been written to the field because if I try and View the data, Access will not let me.  

For reading the databastable, I execute:

char szQuery[256];
CByteArray array ;
BYTE abyteTemp[MAX] ;

CDaoRecordset rs ( &m_db ) ;
sprintf( szQuery, "SELECT * FROM MyTable" ) ;
rs.Open( dbOpenDynaset,szQuery ) ;
if ( !rs.IsEOF( ) )
rs.MoveFirst( );

while( !rs.IsEOF( ) ) {
array.SetSize(m_nSizeOfData) ;

COleVariant v( array ) ;
rs.GetFieldValue( "Data0",v) ;

for (INT n=0;n<m_nSizeOfData;++n)
abyteTemp[n]=array.GetAt(n) ;

rs.MoveNext( ) ;
}

After writing and reading to the database I get garbage values in my CByteArray.  All suggestions welcome and code fragments greatly appreciated!
0
Comment
Question by:Surfer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:Surfer
ID: 1309708
Edited text of question
0
 

Author Comment

by:Surfer
ID: 1309709
Adjusted points to 200
0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 1200 total points
ID: 1309710
I can not make the method you are using work, but here's a way that will work:

Instead of just use a generic CDaoRecordset, derive a new class from
CDaoRecordset (CMySet) using ClassWizard, and bind to the fields in your database table (MyTable).
Bind the field Data0 as a CByteArray called m_rgData0 (it defaults to CLongBinary so remember to change it).

Writing:
   
    // Open recordset
    CMySet rs(&m_db);
    rs.Open(dbOpenDynaset);
   
    // Add new record
    rs.AddNew();
    rs.m_rgData0.SetSize( m_nSizeOfData );
    memcpy( &rs.m_rgData0[0], &abyteTemp[0], m_nSizeOfData );

    // Mark field as having a value and dirty.
    rs.SetFieldNull( &rs.m_rgData0, FALSE );
    rs.SetFieldDirty( &rs.m_rgData0, TRUE );

    // Store record and close recordset
    rs.Update();
    rs.Close();


Reading:

    // Open recordset
    CMySet rs(&m_db);
    rs.Open(dbOpenDynaset);

    // Now recordset is opened and placed on first record.
    // Loop through all records
   
    while( !rs.IsEOF() )
    {
        ASSERT(rs.m_rgData0.GetSize() != 0);

        memcpy( &abyteTemp[0], &rs.m_rgData0[0], rs.m_rgData0.GetSize() );

        // NOTE: call rs.m_rgData0.GetSize() to get size of data
        //       stored in database

        //
        // Now data from 1 record has been copied to abyteTemp
        // Do something with it!
        //

        // Move on to next record
        rs.MoveNext();
    }

    // Close recordset
    rs.Close();


Please note that you should surround these calls with try/catch to catch
errors.
0
 

Author Comment

by:Surfer
ID: 1309711
Your answer was right on the mark!  Thank you very much.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question