1

I am using the SqlFileStream and when constructing the object I am not sure which FileOptions and allocation size to use. I got this from another article but it did not explain why. Can somone help explain or give me a recommendation?

thanks!

using (var destination = new SqlFileStream(serverPathName, serverTxnContext, FileAccess.Write, FileOptions.Asynchronous, 4096))
{
    await file.CopyToAsync(destination);
}
Marco
  • 2,453
  • 3
  • 25
  • 35
  • have you thought about trying a `Google Search` on the Following `C# MSDN SqlFileStream` – MethodMan Jun 09 '16 at 18:58
  • yes i did however it did not answer my questions. – Marco Jun 09 '16 at 19:01
  • try this and maybe you it will shed some light https://msdn.microsoft.com/en-us/library/mt674879.aspx also you understand what the 4096 part is in regards to size / chunks..? also this link explains what all the different methods and property's etc ... do https://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqlfilestream(v=vs.110).aspx – MethodMan Jun 09 '16 at 19:09
  • 4096 - i don't have a good grip on it quite yet – Marco Jun 09 '16 at 19:11
  • what's not to understand.. I would suggest you read up of how to write files in chunks then you will see what that number represents.. and like I have stated in the previous comment.. read the documentation it's right there in front of you. take the time to read it.. – MethodMan Jun 09 '16 at 19:16

1 Answers1

1

Since it appears like you are trying to copy this file asynchronously, you probably want FileOptions.Asynchronous. This the most responsive way to access your file, because you aren't bound to one thread. FileOptions.RandomAccess and FileOptions.SequentialScan both use caching the access the file however FileOptions.SequentialScan isn't guaranteed to cache optimally. Like the name implies, the large difference is how the access the file either randomly or sequentially. The WriteThrough just skips the cache and goes directly to the file which would be faster but riskier.

Allocation size is just the block size on the drive. If you pass 0 it would use the default size, which for an NTFS formatted drive would be 4KB. 4096 turns out to be 4KB so the person here is just making sure the block size is 4KB.

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57
  • You can find more details about AllocationSize here: https://stackoverflow.com/questions/14030171/ntcreatefile-allocationsize (SqlFileStream calls NtCreateFile internally). Also, for example, how does FAT file system driver use it: https://github.com/microsoft/Windows-driver-samples/blob/master/filesys/fastfat/allocsup.c – George Chakhidze Feb 13 '20 at 12:30
  • "The WriteThrough just skips the cache and goes directly to the file which would be faster but riskier." - it would be the other way around. – Thomas Kjørnes Dec 18 '22 at 10:03