The problem is because you are not recording the file contents properly.
By your code, the property Annex class Anexobll is of the type byte[]. At the point in the code you create the SQL string that will be executed in the database, you are simply concatenating the byte[] property with a string. This will not cause the property value to be used, but the framework will use the method internally. Tostring() byte[], which will return the type name. Soon your query would look something like:
insert into tb_anexo (nome_arquivo, anexo) values ('imagem.jpg', 'System.Byte[]')
The correct thing would be to use parameters, not only for the code to work, but because it is good practice and avoids even security problems (SQL Injection). To insert, use the code as follows (adapt to use your connection):
Npgsql.NpgsqlCommand command = new Npgsql.NpgsqlCommand();
command.CommandText = "insert into tb_anexo (nome_arquivo, anexo) values (:nome_arquivo, :anexo)";
command.Parameters.AddWithValue("nome_arquivo", anexo.NomeArquivo);
command.Parameters.AddWithValue("anexo",NpgsqlDbType.Bytea, anexo.Arquivo);
command.ExecuteNonQuery();
I’m guessing you’re using the correct type in your column, which in this case should be bytea (see more in http://www.postgresql.org/docs/9.0/static/datatype-binary.html)
If you are opening a file, why are you using
File.WriteAllBytes()
? It would not be the case to read the file, and not write?– Leonel Sanches da Silva
I don’t think so. I already have this file inside my database in Bytea(Byte[]) what I want now is to write it again (transforms it from byte[] to .jpg) to open.
– Lucas Vasconcelos