jingle
jingle

Reputation: 3

Reading BLOB to byte array in SQLite table

I want to read a blob to a byte array and show the current byte array in the cmd.

This is my code at the moment:

string connector = @"data source=C:\testfile; Version=3;";

SQLiteConnection connection = new SQLiteConnection(connector);
SQLiteCommand command = new SQLiteCommand(connection);

connection.Open();

command.CommandText = "SELECT file FROM users";

SQLiteDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    int i = 0;

    byte[] bytBLOB = new byte[reader.GetBytes(0, 0, null, 0, int.MaxValue) - 1];
    reader.GetBytes(0, 0, bytBLOB, 0, bytBLOB.Length);
    Console.WriteLine(bytBLOB.ToString());

    i++;    
}

Currently my code doesn't work. It just prints out two lines of (I've got two items in "file"):

System.Byte[]
System.Byte[]

How to get my code working. Reading all relevant posts to blob in c# doesn't help me in this situation. I used this help: blogpost

Upvotes: 0

Views: 2959

Answers (1)

TripeHound
TripeHound

Reputation: 2980

.ToString() doesn't work like you expect on a byte[]. You'll need something like:

Console.WriteLine( Encoding.Default.GetString( bytBLOB ) ) ;

assuming the bytes in the blob represent printable characters in the default encoding. If they're random data, you'll need to dump them byte-by-byte.

Upvotes: 1

Related Questions