How to store binary data to SQL database as nvarbinary?



  • Hi,

    I need to store some files to SQL Server database, such as word, excel and pictures. I tried to read binary file data to a QByteArray, but the file contains lots of '\0' byte and this is regarded as the end of the array, so when the QByteArray object can only store a small part of the file, not all the data.
    I think I should first read file into a buffer and then insert the buffer's content to database, but i don't know how to do this use a QSqlQuery, can anybody help me, thanks!

    Thanks!


  • Lifetime Qt Champion

    Hi,

    You can use base64 encoding for you binary data

    Hope it helps



  • Can you tell a lit more please, or giev me a link about this?
    I should still use QByteArray to do this, or use other method?

    [quote author="SGaist" date="1382728116"]Hi,

    You can use base64 encoding for you binary data

    Hope it helps[/quote]



  • I saw this function:
    ByteArray QByteArray::fromBase64(const QByteArray & base64);

    This needs another QByteArray, and the question still exsits. The input QByteArray will be truncated, and only the part before the first '\0' will be copied to the new QByteArray.

    [quote author="SGaist" date="1382728116"]Hi,

    You can use base64 encoding for you binary data

    Hope it helps[/quote]


  • Lifetime Qt Champion

    IIRC, QByteArray handles \0 like any other char, you can verify the size of the array once loaded


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.