How to read and open files from a byte column in SQL Server?

Asked

Viewed 2,442 times

0

My question is the continuation of this question: Link to the other question

I managed to transform my file into bytes and insert it into a table in SQL Server, but now I want it from inside an application I am doing in C# I want to open this file. That is to do reverse engineering (byte > open file).

The code used for insertion:

Anexo a = new Anexo();
FileStream fs = new FileStream(a.Caminho, FileMode.Open, FileAccess.Read);
a.Arquivo = new byte[fs.Length];
fs.Read(a.Arquivo, 0, System.Convert.ToInt32(fs.Length));
string SQL = @"INSERT INTO ANEXO (NOME, CAMINHO, ARQUIVO) VALUES (@nome, @caminho, @arquivo)";
Cmd = new SqlCommand(SQL, Con);
Cmd.Parameters.Add("@nome", SqlDbType.VarChar).Value = a.NomeArquivo;
Cmd.Parameters.Add("@caminho", SqlDbType.VarChar).Value = a.Caminho;
Cmd.Parameters.Add("@arquivo", SqlDbType.VarBinary).Value = a.Arquivo;
Cmd.ExecuteNonQuery();
  • Windows form or Web?

  • C# Windows Forms

  • What file format? you know? PDF, IMAGE, . TXT, will display where?

  • The format is . msg are Outlook emails. Thanks for the replies Paul

  • You will take from the bank and save on the computer, correct?

  • No. It is already in the database... there in my application I have a gridview... and each tuple of this grid will have an attachment of an email in a column with button to open this attachment. Ai at this time comes the byte pass part to the file and dps open.

  • Have you solved it yet? In this code you posted as answer (it was deleted) here: http://answall.com/questions/43713/como-fa%C3%a7o-para-insert-qualquer-exten%C3%A7%C3%a3o-de-file-in-a-sql-database you can see what is wrong. If you’re interested, put in that code that we explained to you how to solve.

Show 2 more comments

2 answers

2

  1. Get the byte array and pass to one MemoryStream in the builder;

  2. Create your FileStream with the desired file name. I assume you are saving at least the original file extension (the name is optional, but if you mix different files, it is worth saving a column with the ". eml", ". txt", etc);

  3. Read the bytes of MemoryStream and save in FileStream;

  4. If you are going to open the file, you can call the Process.Start.

// aqui está seu array de bytes
byte[] arquivo;
const int BufferSize = 65536;

// create memory stream
using (var mstrm = new MemoryStream(arquivo))
{
    using (var outStream = File.Create("resultado.eml"))
    {
        var buffer = new byte[BufferSize];
        int bytesLidos;
        while ((bytesLidos = mstrm.Read(buffer, 0, BufferSize)) != 0)
        {
            outStream.Write(buffer, 0, bytesLidos);
        }
    }
}

Editing

Use the following code to read the file in its byte array:

public byte[] FileToByteArray(string fileName)
{
    byte[] buff = null;
    FileStream fs = new FileStream(fileName, 
                               FileMode.Open, 
                               FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    long numBytes = new FileInfo(fileName).Length;
    buff = br.ReadBytes((int) numBytes);
    return buff;
}

It ensures that you can read large files! I know your files should not be too large, but if they have large attachments they may give problem.

To record, if not working, you can also use the way @Intruder pointed out as well:

File.WriteAllBytes(string caminho, byte[] array)
  • Hello Rodrigo. Thank you for your reply helped me. But the file is saved in the directory but when I open it the only thing that comes inside it is this: "System.Byte[]" ....

  • @Lucasvasconcelos can post the code of how you are recording the column?

  • I added in the post!! I was unable to add here with indentation.

  • @Lucasvasconcelos try to see if this helps you move forward.

1

If your array represents a complete file:

File.WriteAllBytes(string path, byte[] bytes)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.