Binary data with SQLite and C#

  • Thread starter Alexander Stuckenholz
  • Start date
A

Alexander Stuckenholz

Hello.

I´m using the ado.net provider from sourceforge for SQLite database.
Till now everything works fine. But today i tried to enter some binary
data into
one row.

Here´s my code:

command.CommandText = "INSERT INTO _files VALUES ('" + obj.ID + "','"
+ this.GetTimestamp() + "',?,'" + obj.DataLength + "','" +
obj.Filename + "')";
command.Parameters[0].DbType = System.Data.DbType.Binary;
command.Parameters[0].Value = (object) obj.Data;

The row in the table has the type LONGBINARY.
SQLite just inserts the string representation of the byte array into
the
row but not the data!

Any ideas?

Alex
 
E

Egbert Nierop \(MVP for IIS\)

Alexander Stuckenholz said:
Hello.

I´m using the ado.net provider from sourceforge for SQLite database.
Till now everything works fine. But today i tried to enter some binary
data into
one row.

Here´s my code:

command.CommandText = "INSERT INTO _files VALUES ('" + obj.ID + "','"
+ this.GetTimestamp() + "',?,'" + obj.DataLength + "','" +
obj.Filename + "')";
command.Parameters[0].DbType = System.Data.DbType.Binary;
command.Parameters[0].Value = (object) obj.Data;

I would expect this
command.Parameters[0].Value = (byte[]) obj.Data;
The row in the table has the type LONGBINARY.
SQLite just inserts the string representation of the byte array into
the
row but not the data!

many drivers do not encode binary data. If you find that the driver does not
do it, make the data type DbType.String (or so)
and format the string as hexadecimal (from the byte array) and append 0x
before the string.
 
A

Alexander Stuckenholz

I would expect this
command.Parameters[0].Value = (byte[]) obj.Data;

The property ob.Data already delivers byte[]. When i assign this to
the
value property of the parameter, the same happens as described. Just
the string representation will saved into the database.

I will try to do that workaround.

Thanx.

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top