I am currently learning how to store files without using FILESTREAM attribute on varchar(max) in SQL Server, because I dont need to store very large binary files. Despite that I want to store the binary data by small chunks of it. What I found was the SqlParameter.Offset Property. Here is some info about it from MSDN:
"The Offset property is used for client-side chunking of binary and string data. For example, in order to insert 10MB of text into a column on a server, a user might execute 10 parameterized inserts of 1MB chunks, shifting the value of Offset on each iteration by 1MB."
It sounds like it is exactly what I need, but I am confused about how to use it. I created a very simple Table called BinaryFilesTable (Id, FileData).The Id is the primary key and FileData is varbinary(max). Here is my code so far.
public static void Main(string[] args)
{
var fileBytes = File.ReadAllBytes("../../image.jpg");
const string connectionString = @"Server=.; Database=TestDb; Integrated Security=true";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
using (connection)
{
string commandText = "INSERT INTO BinaryFilesTable(FileData) VALUES (@binaryFileData)";
SqlCommand command = new SqlCommand(commandText, connection);
SqlParameter commandParameter = new SqlParameter();
commandParameter.ParameterName = "@binaryFileData";
commandParameter.Value = fileBytes;
commandParameter.SqlDbType = SqlDbType.VarBinary;
commandParameter.Size = -1; // VarBinary becomes VarBinary(max) if size is set to -1.
//commandParameter.Offset = ??? How to use it ???
command.Parameters.Add(commandParameter);
command.ExecuteNonQuery();
}
}
With this code I managed to store the data in the table, but I am not exactly sure how it works. As I understand it, in the moment the binary data is stored all at once and that is the reason I want to figure out how to use this Offset property. I spent several hours searching for a tutorial but had no success.
EDIT : If I set some value to the offset I get the following exception:
System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.