Click here to Skip to main content
15,890,690 members
Articles / Database Development / SQL Server / SQL Server 2008R2
Tip/Trick

Storing Large Binary Data in a SQLSERVER Database

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
5 Oct 2014CPOL 12.2K   2   2
How to write data to SQLSERVER varbinary(MAX) columns in C++ using ADO

Introduction

I had to store rows of several fairly large multidimensional numerical arrays in a SQLSERVER database. These data must be exportable from there to a mySQL database as well. Since it is absolutely impossible (and fortunately not necessary) to have a separate column for each value, I decided to store them as a blob.

Background

Although this is not a big problem using ODBC, I did not find explicit examples of how to do this using C++ and ADO anywhere (perhaps my fault...).

Using the Code

All arrays/variables in question are serialized into a single unsigned char array using memcpy which is copied into a SAFEARRAY afterwards.

The SAFEARRAY is attached to a _variant_t variable of type VT_ARRAY | VT_UI1.

This _variant_t variable can now be stored in a varbinary(MAX) column in a SQLSERVER database using the ADO PutValue method:

C++
// let no_of_Bytes be the size of the array in bytes
// let myArray be the name of the array
// let outrecordset be an open ADO recordset with col_name being the name of the respective column

unsigned char *U=new unsigned char[no_of_Bytes]; // perhaps larger, see below
memcpy(&U[0],myArray,no_of_Bytes); // more arrays/variables may follow

SAFEARRAY *SA=SafeArrayCreateVector(VT_UI1,0,no_of_Bytes);
long ix[1]; // array of indexes
for (int i=0;i<no_of_Bytes;i++)
{
    ix[0]=i; // first and only index
    SafeArrayPutElement(SA,ix,&U[i]);
}

_variant_t *V=new _variant_t();
V->vt=VT_ARRAY|VT_UI1; // Type of variable
V->parray=SA;  // attach SAFEARRAY

outrecordset->AddNew(); // new record
outrecordset->Fields->GetItem("col_name")->PutValue(V); // store
outrecordset->UpdateBatch(adAffectAll); // and update

delete [] U;  // clean up
SafeArrayDelete(SA);
V->parray=NULL; // deleting V may throw an exception otherwise
delete V;

Of course, instead of arrays, any binary data that fits into the varbinary(MAX) size limit can be handled like this. Reading the data from the database into a SAFEARRAY is done using GetValue().

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerNeleks suggestions did work very well Pin
Schrödter6-Oct-14 1:53
Schrödter6-Oct-14 1:53 
QuestionAnswer to your private email about code format Pin
Nelek5-Oct-14 23:16
protectorNelek5-Oct-14 23:16 
Hi,

first of all. Please use the "reply" link not the "email". That way your message appears in the board and is easier to follow the "conversation"

Regarding the format problem. Code project has a special format tool (see the "code" button on the top of the message window / edit box). With it you can choose different languages to format your code.
Examples for option 1 below:
// this is not formated
if (code lang == nothing)
{
//No indentation, no colours...
}
C#
// this is c#
if (code lang == c#)
{
   //indentation remains, colours get adapted...
   int myInt = new whatever; 
}

VB
' this is vb
if (code lang == c#) then
{
   'indentation remains, colours get adapted...
   dim something as whatever;
}

SQL
--this is SQL
Select something from table T where whatever

   this is coded as text
There is no colours, but
       the different indentation still remains
  good for "drawing"

Column_1   Column_2   Column_3
value_01   value_02   value_03
   X           Y         Z


You can do something like that if you first paste your code, from the pop-up on the right side you choose "paste as is". Once the content is pasted, you select it with the mouse, click in the "code" button above and select the desired language.

When you paste something you can choose as well, "quoted text" as this example:


Now I am going to copy and paste the different html format tags using the "encode html-tag" option of the right popup
- Examples for option 2 below:
<pre lang="c#"> here is the sample snippet </pre>
<pre lang="VB"> here is the sample snippet </pre>
<pre lang="SQL"> here is the sample snippet </pre>
<pre lang="Text"> here is the sample snippet </pre>
<blockquote class="quote"><div class="op">Quote:</div>here is the quoted text</blockquote>

In order to repair your snippet:
You should edit your post, delete the messed up part, copy it from the source, paste it another time in the wizard, choose "paste as is" from the right pop up, and...
Option 1) select it with the mouse, go to the button "code" above the window, click it and choose the desired language
Option 2) just write the pre lang ="whatever" as described above before your code and the /pre to end the format at the end of your code.

I hope I could help you.

If you don't understand something, do not hesitate to ask suing the "reply"

In addition, if you have any question about articles, tips and how to post new content into CP, you might ask in the special forum
http://www.codeproject.com/Forums/1641/Article-Writing.aspx[^]
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.