The question is entitled with the word ‘Stream’ because the question below is a concrete example of a more generic doubt I have about Streams:
I have a problem that accepts two solutions and I want to know the best one:
- I download a file, save it to disk (2 min), read it and write the contents to the DB (+ 2 min).
- I download a file and write the contents directly to the DB (3 min).
If the write to DB fails I’ll have to download again in the second case, but not in the first case.
Which is best? Which would you use?
To detail Jekke’s reply:
Depending on the file system creates many occasions for failure (you must create a valid file name, make sure the file system isn’t full, make sure the file can be opened and written to by you but not by anyone else, what about concurrent use, etcetera).
The only benefit of writing to file I can think of is that you’ll know the download completed successfully prior to doing anything with the database. If you can hold the contents in memory, do that instead. If you can’t and really insist on not going to the database in case of an interrrupted download, at least use .NET’s built-in support to help you with the tricky bits (e.g. IsolatedStorageFileStream).