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 7438295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:31:55+00:00 2026-05-29T10:31:55+00:00

I am playing with the SqlFileStream to store my (large) objects on a SQL

  • 0

I am playing with theSqlFileStream to store my (large) objects on a SQL Server (in a varbinary(max) column), but i can’t really get a it to do as i want to. I have been trying to find some articles specific for serializing and storing (not just reading files from disk to store) and how to do this most efficiently. Is there some “hidden” best practice articles that I just haven’t found yet? Or do someone have a nice example of how to do this?

My primary source is Using SqlFileStream with C# to Access SQL Server FILESTREAM Data

Edit:

Added example code, where the object DataObject must be stored in a SQL Server table using filestream. I expect that it would be possible to stream my object, into the database but i currently don’t know how to this in the best way, what do i need to do to the object to stream it and have in mind that the object can be >1 GB with very complex structures in it.

New edit:
I have tested it with data structures containing very little data <300 kb and there are no problems. Then i’m thinking if it is a buffer problem or similar?

using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

namespace QEStore
{
    public class DataObject
    {
        private Dictionary<int, string> _values = new Dictionary<int, string>();
        private string _objName;
    }

    public class Store
    {
        private string _connStr = "Data Source=127.0.0.1;Integrated Security=True;Initial Catalog=my_data;";

        public void InsertObject(int id, DataObject data)
        {
            var insStmt =
                @"insert into data (data_id) values (@dataID);
                  select data_value.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() from data where data_id = @dataID;";

            string serverPath;
            byte[] serverTxn;

            using (var ts = new TransactionScope())
            {
                using (var conn = new SqlConnection(this._connStr))
                {
                    conn.Open();
                    using (var cmd = new SqlCommand(insStmt, conn))
                    {
                        cmd.Parameters.Add("@dataID", SqlDbType.Int).Value = id;
                        using (var reader = cmd.ExecuteReader())
                        {
                            reader.Read();
                            serverPath = reader.GetSqlString(0).Value;
                            serverTxn = reader.GetSqlBinary(1).Value;
                            reader.Close();
                        }
                    }
                    using (var dest = new SqlFileStream(serverPath, serverTxn, FileAccess.Write))
                    {
                        // How to write the DataObject to the database using SqlFileStream
                        // dest.Write(...);
                    }
                }
                ts.Complete();
            }
        }
    }
}

I have tried serializing the object as following, but get an IOException error saying “The handle is invalid”:

using (var dest = new SqlFileStream(serverPath, serverTxn, FileAccess.Write))
{
    var formatter = new BinaryFormatter();
    formatter.Serialize(dest, data);
    dest.Close();
}
  • 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-05-29T10:31:55+00:00Added an answer on May 29, 2026 at 10:31 am

    I found the problem. The problem was that the build-in serializer could not handle the large data object, using protobuf-net instead worked.

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

Sidebar

Related Questions

Playing with a few tutorials on sockets but struggling to connect. My tomcat server
been playing around with Capistrano to get an automated deploy between my server and
Playing with the new(ish) url rewriting functionality for web forms, but I'm running into
Playing with jquery for the first time, and I'm trying to get a simple
been playing around with ResolveClientUrl(~/Confirmation.aspx) and other methods.. I am tryin go get the
been playing with this for a few hours and can't figure it out. jsFiddle
Im playing around with TryParse() But lets say the parsing fails, then returns false,
Playing with Disqus for commenting, but the problem is that we have a Silverlight
Busy playing with django, but one thing seems to be tripping me up is
playing around with this and cannot get a grip on how to connect with

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.