Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Code snippets: binary data insertion

2004-10-11
2013-04-15
  • Hello!

    Can someone give me a code snippet, how I can insert binary data into a table?

    Thnx: ReLuXa from Hungary

     
    • I've solved my problem...

       
      • Peter Bromberg
        Peter Bromberg
        2004-10-19

        Well, Thanks for telling everyone all about your discovery! The only way I have been successful with images is to convert to Base64 string going in, and back out.

         
        • it seems that transcation is invalid when insert blob.

          if you insert blob item a/b/c/d/e/f... into a colonm, every row of the colonm will be a.

           
      • Here is the snippet:

        IDbCommand cmd = connection.CreateCommand();
        cmd.CommandText = "CREATE TABLE Tbl (t BLOB)";
        cmd.ExecuteNonQuery();

        byte[] someValues = {0x01,0x02};
        cmd.CommandText = "INSERT INTO Tbl (t) VALUES (@t)";
        IDbDataParameter p = cmd.CreateParameter();
        p.DbType = DbType.Binary;
        p.Value = someValues;
        cmd.Paramaters.Add(p);
        cmd.ExecuteNonQuery();