Saving an array to a SQL Server table with VB and ADO

Saving an array to a SQL Server table with VB and ADO

Persisting an array of data in a SQL Server DB is usually considered quite difficult. The common solution is to use a master detail table, but this leads to a waste of space and overall decreases performance. Here we’ll see how to save an array with VB and ADO.Create the destination table with the following T-SQL code:

CREATE TABLE tblArrays (    ID numeric(18, 0) IDENTITY (1, 1) NOT NULL ,    Name char (32) NULL ,    Data image NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GO

The field named “Data” is an Image field that can store Variant or Object data. The VB class presented in this article stores an array into a PropertyBag variable, and then persists this object to the DB table. For the data read, it performs the opposite operation: the data stream is saved into a Variant variable, which represents the PropertyBag with the array. Here’s the code of the class:

' Class name: CDBVariant' Database variant class by Giuseppe Dimauro in 2000Dim mCn As ADODB.Connection' set the connection to be used by the classPublic Property Set DataSource(newVal As ADODB.Connection)    Set mCn = newValEnd Property' read the Variant from the DB' tblName = source table name' Name = variable name to be readPublic Property Get DBVariant(tblName As String, Name As String) As Variant    Dim strFilter As String, pb As New PropertyBag, vtTemp As Variant    Dim rs As New ADODB.Recordset    strFilter = "Name = '" & Name & "'"    rs.Open "SELECT * FROM " & tblName & " WHERE " & strFilter, mCn, _        adOpenStatic, adLockPessimistic        If Not rs.EOF And Not rs.BOF Then        vtTemp = rs!Data        pb.Contents = vtTemp        If pb.ReadProperty("Size", 0) = 0 Then            DBVariant = pb.ReadProperty("Value", vbNull)        Else            Dim vtTemp2() As Variant            ReDim vtTemp2(pb.ReadProperty("Size", 0)) As Variant            For i = 0 To UBound(vtTemp2)                vtTemp2(i) = pb.ReadProperty(CStr(i))            Next            DBVariant = vtTemp2        End If    End If        Set rs = NothingEnd Property' write the Variant to the DB' tblName = destination table name' Name = variable namePublic Property Let DBVariant(tblName As String, Name As String, _    newValue As Variant)    Dim strFilter As String, pb As New PropertyBag    Dim rs As New ADODB.Recordset    Dim bRemove As Boolean    strFilter = "Name = '" & Name & "'"    rs.Open "SELECT * FROM " & tblName & " WHERE " & strFilter, mCn, _        adOpenStatic, adLockPessimistic            If VarType(newValue) = vbObject Then        bRemove = newValue Is Nothing    Else        bRemove = False    End If        If Not bRemove Then        If rs.EOF And rs.BOF Then            rs.AddNew            rs!Name = Name        End If        If (VarType(newValue) And vbArray) = 0 Then            ' valore non array            pb.WriteProperty "Size", 0            pb.WriteProperty "Value", newValue        Else            ' preparazione di uno stream per array            pb.WriteProperty "Size", UBound(newValue)            For i = 0 To UBound(newValue)                pb.WriteProperty CStr(i), newValue(i)            Next        End If        rs!Data = CVar(pb.Contents)        rs.Update    Else        If Not rs.EOF And Not rs.BOF Then            rs.Delete        End If    End If    Set rs = NothingEnd Property

This code shows how to use the class:

Dim t As New cDBVariantDim cn As New ADODB.ConnectionDim vArray(100) As IntegerDim vArray2 As Variant' open a connectioncn.Open "file name=c:udlmaster.udl"For i = 1 To UBound(vArray)    vArray(i) = iNext' assign the connection to the classSet t.DataSource = cn' write the arrayt.DBVariant("tblArrays", "vArray") = vArray' read the arrayvArray2 = t.DBVariant("tblArrays", "vArray")' dump resultsDebug.Print UBound(vArray2)For i = 1 To UBound(vArray2)    Debug.Print vArray2(i)Next

Note that the .UDL file is a data link file, but of course you can use any connection string.

########################################################
This tip has been originally published on Microsoft Italia’s web site. It has been translated and re-published on VB2TheMax with the permission of Microsoft Italia. You can find more tips like this one (in Italian) at http://www.microsoft.com/italy/sql/articoli
########################################################

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as