Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:20:53+00:00 2026-06-11T09:20:53+00:00

I pulled a SafeFileHandle via some P/Invoke. Here’s a snippet: [DllImport(kernel32.dll, SetLastError = true,

  • 0

I pulled a SafeFileHandle via some P/Invoke. Here’s a snippet:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern SafeFileHandle CreateFile(
        string lpFileName,
        EFileAccess dwDesiredAccess,
        EFileShare dwShareMode,
        IntPtr lpSecurityAttributes,
        ECreationDisposition dwCreationDisposition,
        EFileAttributes dwFlagsAndAttributes,
        IntPtr hTemplateFile);

Then I can grab the handle pretty easily:

SafeFileHandle fileHandle = CreateFile(path, EFileAccess.GenericRead, EFileShare.None, IntPtr.Zero,ECreationDisposition.OpenExisting, 0, IntPtr.Zero);

I have to do this because System.IO.File only takes a string path and I need to support fully qualified paths greater than the 260 character MAX_PATH.

Anyways, I have a command procedure on sql server to perform an insert. I am currently adding the byte array of the stream to a varbinary(max) column (most of my files are less than 2MB so filestream didn’t seem worth going to).

I’m currently populating the command parameter like so:

using (FileStream s = new FileStream(fileHandle, FileAccess.Read))
{
    byte[] buf = new byte[s.Length];
    s.Read(buf, 0, Convert.ToInt32(s.Length));
    cmd.Parameters.AddWithValue("@File", buf);
}

It just seems really slow compared to the usual File.ReadAllBytes that I could use with a file path less than MAX_PATH. Is there a more optimal way to put this binary into the DB?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:20:54+00:00Added an answer on June 11, 2026 at 9:20 am

    Pass FILE_FLAG_NO_BUFFERING flag in dwFlagsAndAttributes. Since you are reading the entire file in one go, the buffering that is built into native Windows IO is unnecessary and adds a little bit of overhead. Not sure how much it’ll save you, but sometimes every little bit helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple pages being pulled together into a single page. Some of these
I pulled up the NWmatcher source code for some light morning reading and noticed
I've accidentally pulled some changes from the main repo with --rebase parameter. How do
I've just pulled a new branch, made some local changes, committed and tried to
I have some data that gets pulled out of a database and mapped to
I am trying to write some data pulled from some source code (using httplib2
Items of a ComboBox are getting pulled from some DB cache,etc.. This ComboBox has
I pulled a remote branch. I made some changes. I want to push my
Pulled down an old revision of a project from subversion, made some changes, and
I've pulled some facebook statusses to my website and I would like to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.