Patrik Schweigl
Patrik Schweigl

Reputation: 93

Stream data into sql server database without buffering the whole data

I have a Table Blob, which has a varbinary(max) as a column. Now I want to store data into the database using a Filestream. The data can be really big (in my case 1.5GB) so I dont want to load the whole data into a buffer.

What I tried:

        using (FileStream fs = File.Open(@"BigData.iso", FileMode.Open))
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = @"...";
                conn.Open();
                SqlCommand command = new SqlCommand("INSERT INTO Blob Values (@0, @1)", conn);
                command.Parameters.Add(new SqlParameter("0", Guid.NewGuid()));
                var sqlb = new SqlBytes(fs);
                command.Parameters.Add(new SqlParameter("1", SqlDbType.VarBinary, -1)).Value = sqlb;
                command.ExecuteNonQuery();
            }
        }

But I got an OutOfMemoryException, because SqlBytes initialze its buffer to the whole size of the data.

I know there is a FILESTREAM feature from Microsoft, but I don't want to use it.

Is there a way to achieve this?

Upvotes: 4

Views: 1940

Answers (1)

Serge
Serge

Reputation: 4036

You can read the file in small chunks and append them to the data column.

You will need an IDENTITY column or another column(s) that can be used as a key to execute UPDATE statements. Here is an example using an IDENTITY column:

Create a table to store the data

CREATE TABLE [dbo].[table1](
    [ID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
    [Data] [varbinary](max) NULL,
)

Implement C# to insert/update data in chunks

private const string C_SqlConnectionString = @"Server=SERVERNAME;Database=DBNAME;Trusted_Connection=yes;";
private const int C_FileChunkSizeBytes = 1024 * 1024; // 1 MB

private static void storeFile(string filepath)
{
    using (FileStream fs = File.Open(filepath, FileMode.Open))
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = C_SqlConnectionString;
            conn.Open();

            // Use a transaction to ensure that all parts of the file get stored to DB
            SqlCommand command = new SqlCommand("BEGIN TRAN", conn);
            command.ExecuteNonQuery();

            var pos = 0;
            byte[] fileBytes = null;
            int sqlRowId = 0;

            // Read the file in chunks
            while (pos < fs.Length)
            {
                // Read file bytes
                var bytesToRead = pos + C_FileChunkSizeBytes < fs.Length
                    ? C_FileChunkSizeBytes
                    : (int)(fs.Length - pos);

                fileBytes = new byte[bytesToRead];
                fs.Read(fileBytes, 0, bytesToRead);

                // Store bytes to a parameter
                var varbinary = new SqlParameter("0", System.Data.SqlDbType.VarBinary, -1);
                varbinary.Value = fileBytes;

                if (pos == 0)
                {
                    // If this is the first chunk, then we need to INSERT
                    // The HOLDLOCK hint will hold a lock on the table until transaction completes (or is rolled back)
                    command = new SqlCommand("INSERT INTO [dbo].[table1] WITH(HOLDLOCK) VALUES(@0)", conn);
                    command.Parameters.Add(varbinary);
                    command.ExecuteNonQuery();

                    // Get the row ID for the inserted row
                    command = new SqlCommand("SELECT @@IDENTITY", conn);
                    sqlRowId = Convert.ToInt32(command.ExecuteScalar());
                }
                else
                {
                    // Update existing row and append data
                    command = new SqlCommand("UPDATE [dbo].[table1] SET [Data] = [Data] + @0 WHERE [ID] = @1", conn);
                    command.Parameters.Add(varbinary);
                    command.Parameters.Add(new SqlParameter("1", System.Data.SqlDbType.Int)).Value = sqlRowId;
                    command.ExecuteNonQuery();
                }

                // ** Good place for a breakpoint
                pos += bytesToRead;
            }

            // Commit transaction
            command = new SqlCommand("COMMIT TRAN", conn);
            command.ExecuteNonQuery();

            conn.Close();
        }
    }
}

Testing

Place a breakpoint in C# code at the bottom of the while loop, eg at pos += bytesToRead;.

Whilst executing the code, when code execution stops at the breakpoint, check the data in SQL:

SELECT *
        ,LEN([Data]) AS [Length]
FROM [dbo].[table1] WITH(NOLOCK)

The NOLOCK hint will let us see data in uncommitted transactions. LEN([Data]) will show how field length grows after each iteration of the while loop.

Upvotes: 1

Related Questions