c# - How to use the SqlParameter.Offset property and ADO.NET to store a binary file in SQL Server 2012 -
i learning how store files without using filestream attribute on varchar(max) in sql server, because dont need store large binary files. despite want store binary data small chunks of it. found sqlparameter.offset property. here info msdn:
"the offset property used client-side chunking of binary , string data. example, in order insert 10mb of text column on server, user might execute 10 parameterized inserts of 1mb chunks, shifting value of offset on each iteration 1mb."
it sounds need, confused how use it. created simple table called binaryfilestable (id, filedata).the id primary key , filedata varbinary(max). here code 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 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 set -1. //commandparameter.offset = ??? how use ??? command.parameters.add(commandparameter); command.executenonquery(); } }
with code managed store data in table, not sure how works. understand it, in moment binary data stored @ once , reason want figure out how use offset property. spent several hours searching tutorial had no success.
edit : if set value offset following exception:
system.argumentexception: offset , length out of bounds array or count greater number of elements index end of source collection.
the offset
property used client-side chunking of binary , string data.
for example, in order insert 10mb of text column on server, user might execute 10 parameterized inserts of 1mb chunks, shifting value of offset on each iteration 1mb.
offset specifies number of bytes binary types, , number of characters strings. count of strings not include terminating character.
//simple use offset parameter.offset = 3;
Comments
Post a Comment