0

I'm trying to convert a .db file to binary so I can stream it across a web server. I'm pretty new to C#. I've gotten as far as looking at code snippets online but I'm not really sure if the code below puts me on the right track. How I can write the data once I read it? Does BinaryReader automatically open up and read the entire file so I can then just write it out in binary format?

class Program
{
    static void Main(string[] args)
    {
        using (FileStream fs = new FileStream("output.bin", FileMode.Create))
        {
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                long totalBytes = new System.IO.FileInfo("input.db").Length;
                byte[] buffer = null;

                BinaryReader binReader = new BinaryReader(File.Open("input.db", FileMode.Open)); 
            }
        }
    }
}

Edit: Code to stream the database:

[WebGet(UriTemplate = "GetDatabase/{databaseName}")]
public Stream GetDatabase(string databaseName)
{
    string fileName = "\\\\computer\\" + databaseName + ".db";

    if (File.Exists(fileName))
    {
        FileStream stream = File.OpenRead(fileName);

        if (WebOperationContext.Current != null)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "binary/.bin";
        }

        return stream;
    }

    return null;
}

When I call my server, I get nothing back. When I use this same type of method for a content-type of image/.png, it works fine.

4
  • 1
    What do you mean by convert to binary? What is format of input.db? Commented Aug 7, 2012 at 16:53
  • @Audrey .db is a format I can open in Sqlite Manager. Are there other formats used for databases for sqlite? I'm pretty new to this area. Thanks! Commented Aug 7, 2012 at 16:59
  • could you please explain what is your ultimate goal? SQLite databases are binary itself. Commented Aug 7, 2012 at 17:00
  • @Audrey My ultimate goal is to just stream the sqlite database itself from the server to a device. I added code what I have for that above. Commented Aug 7, 2012 at 17:02

1 Answer 1

2

All the code you posted will actually do is copy the file input.db to the file output.bin. You could accomplish the same using File.Copy.

BinaryReader will just read in all of the bytes of the file. It is a suitable start to streaming the bytes to an output stream that expects binary data.

Once you have the bytes corresponding to your file, you can write them to the web server's response like this:

using (BinaryReader binReader = new BinaryReader(File.Open("input.db", 
                                                 FileMode.Open))) 
{
    byte[] bytes = binReader.ReadBytes(int.MaxValue); // See note below
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.Close();
    Response.End();
}

Note: The code binReader.ReadBytes(int.MaxValue) is for demonstrating the concept only. Don't use it in production code as loading a large file can quickly lead to an OutOfMemoryException. Instead, you should read in the file in chunks, writing to the response stream in chunks.

See this answer for guidance on how to do that

https://stackoverflow.com/a/8613300/141172

Sign up to request clarification or add additional context in comments.

2 Comments

Eeeem, read1.ReadBytes(int.MaxValue) is dangerous idea. May be use Stream.Copy new in net 4.0 or chucked coping?
Yeah I know. Updated the answer to show how to do it in chunks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.