StoneSour
StoneSour

Reputation: 11

insert binary data into firebird BLOB field

i'm try record pdf files into the firebird databse, but in the firebird the field is type BLOB and C# code haven't this option. Follow my code:

public void databaseFilePut(string varFilePath) {

            OleDbConnection cn = new OleDbConnection("MinhaConexao");
            byte[] imagem;

            FileStream fs = new FileStream(varFilePath, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(varFilePath).Length;
            imagem = br.ReadBytes((int)numBytes);
            string _sql = "insert into GD_ARQUIVODOC (GD_ARQDOCARQUIVO) values (?)";
            cn.Open();
            OleDbCommand cmd = new OleDbCommand(_sql, cn);   
            cmd.Parameters.Add("?", OleDbType.VarBinary).Value = imagem;
            cmd.ExecuteNonQuery();
            cn.Close();

Whats wrong with this? the error is: Parameter value is not readable. Why? someone can help me ? ty for now

Upvotes: 1

Views: 4146

Answers (2)

Dmitry Kovalenko
Dmitry Kovalenko

Reputation: 1

Allow me to use our own .NET Provider in this answer :)

using System;
using System.Data;
using lcpi.data.oledb; //LCPI .NET Data Provider for OLEDB

namespace ConsoleApplication1
{
 class Program
 {
  private const string c_cn_str
   ="provider=LCPI.IBProvider.3;"
   +"location=localhost:d:\\database\\ibp_test_fb25_d3.gdb;"
   +"user id=gamer;"
   +"password=vermut;"
   +"dbclient_library=fbclient.dll";

  static void Main(string[] args)
  {
   try
   {
    using(var cn=new OleDbConnection(c_cn_str))
    {
     cn.Open();

     using(var tr=cn.BeginTransaction(IsolationLevel.RepeatableRead))
     {
      using(var cmd=new OleDbCommand("",cn,tr))
      {
       cmd.CommandText
        ="insert into BIN_BLOB_TABLE (BIN_DATA) values(:bin)\n"
         +"returning TEST_ID\n"
         +"into :id";

       cmd["bin"].Value=new byte[]{1,2,3};

       cmd.ExecuteNonQuery();

       var rec_id=cmd["id"].Value;

       cmd.CommandText
        ="select BIN_DATA from BIN_BLOB_TABLE where TEST_ID=:x";

       cmd["x"].Value=rec_id;

       var data=(byte[])cmd.ExecuteScalar();

       for(int i=0;i!=data.Length;++i)
        Console.WriteLine("[{0}]: {1}",i,data[i]);
      }//using cmd

      tr.Commit();
     }//using tr
    }//using cn
   }
   catch(Exception e)
   {
    Console.WriteLine("ERROR: {0} - {1}",e.Source,e.Message);
   }//catch
  }//Main
 }//Program
}//namespace ConsoleApplication1

Upvotes: 0

Mark Rotteveel
Mark Rotteveel

Reputation: 109173

Check this example on the IBProvider site:

using (OleDbConnection con = ConnectionProvider.CreateConnection())
{
    con.Open();
    OleDbTransaction trans = con.BeginTransaction ();
    OleDbCommand cmd = new OleDbCommand(...);
    //....
    OleDbParameter blob = new OleDbParameter();
    blob.OleDbType = OleDbType.LongVarBinary;
    blob.Value = /// a byte[]
    cmd.Parameters.Add(blob);
    cmd.ExecuteNonQuery();
    trans.Commit();
}

Upvotes: 0

Related Questions