Binary data with SQLite and C#

  • Thread starter Thread starter Alexander Stuckenholz
  • Start date 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
 
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.
 
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
 
Back
Top