Question

I have a database with date fields that "MUST" be encrypted.

To decrypt directly from the database, I use this:

    Dim comm As New SqlCommand()
    Dim dt As New DataTable

    comm.Connection = conn ' connection assignment to sql cmd

    With comm
        .CommandText = "SELECT * FROM EMPL ORDER BY EMPL_FIRST_NM ASC"
    End With

    Dim adapter As New SqlDataAdapter(comm)
    adapter.Fill(dt)                    'Fill DT with Query results

    DataGridView1.DataSource = dt       'fill DGV

    Try
        For i As Integer = 0 To dt.Rows.Count - 1
            dt.Rows(i)(1) = clsEncrypt.DecryptData(dt.Rows(i)(1))
            dt.Rows(i)(2) = clsEncrypt.DecryptData(dt.Rows(i)(2))
            And so on..
        Next
    Catch ex As Exception
        MessageBox.Show(e.ToString())
    End Try

My situation: I need to run a WHERE clause against specific date ranges. So, currently in my DGV, Columns 19 & 20 are BeginDate and EndDate.

If I need to pull back a query, like, SELECT EMPL_FIRST_NM, EMPL_LAST_NM FROM ???? WHERE BEGINDATE >= 12/21/2013 I would need to look at the decrypted date values.

I have seen something like:

Dim dr As DataRow() dr = 

But I am not sure for my specific scenario.

For a better visual: In the DataTable that I populate my DGV with (some rows omitted)

+-----------------------------------------------+
| EMP_ID  EMP_F_NAME  EMP_L_NAME BEG_DT  END_DT |
+-----------------------------------------------+
| 100      John        Doe     20140101 24000101|
| 200      Jake        Locke   20070101 24000101|
| 300      Jim         Slim    20120101 24000101|
| 400      Javier      Suave   20100101 24000101|
+-----------------------------------------------+

What it looks like in the Db:

+------------------------------------------------+
| EMP_ID  EMP_F_NAME  EMP_L_NAME BEG_DT  END_DT  |
+------------------------------------------------+
| ^##$D      @3sAdfq   MR%      $@GFgeh $%@YYWEG |
| K&^D@      54F#$3    L:er@#   %$@YG&^ NH#%HJBR |
| D!@#$      RGER454   M$#Rz    $%T@GERG hYE76F& |
| vfbDW[     DQWR5rf   ~gE5yb   #$!TDDg  mHY6$1* |
+------------------------------------------------+
Was it helpful?

Solution 2

So, I'm not sure of the DataRow approach because I wasn't sure how to use it. It's partially my fault because I forgot to mention in my OP that I will need to actually be able to use this data, whether it be for a report or showing data in DGV.

That being said, I found code that led me to use a Structure. I've never used one before, so this could be entirely wrong - but it works.

    'Define a structure to be used as source for data grid view.
Structure mystructure
    Private mDim1 As String
    Private mDim2 As String
    Private mDim3 As String
    Private mDim4 As String
    Private mDim5 As String
    Public Property Dim1() As String
        Get
            Return mDim1
        End Get
        Set(ByVal value As String)
            mDim1 = value
        End Set
    End Property
    Public Property Dim2() As String
        Get
            Return mDim2
        End Get
        Set(ByVal value As String)
            mDim2 = value
        End Set
    End Property
    Public Property Dim3() As String
        Get
            Return mDim3
        End Get
        Set(ByVal value As String)
            mDim3 = value
        End Set
    End Property
    Public Property Dim4() As String
        Get
            Return mDim4
        End Get
        Set(ByVal value As String)
            mDim4 = value
        End Set
    End Property
    Public Property Dim5() As String
        Get
            Return mDim5
        End Get
        Set(ByVal value As String)
            mDim5 = value
        End Set
    End Property
End Structure

Now that we have the structure ready to accept the values we pass into it..

Dim lCount As Integer = 0
Dim dt As New DataTable
    With comm
        .CommandText = "SELECT EMPL_ID, EMPL_FIRST_NM, EMPL_LAST_NM, BEG_DT, END_DT FROM EMPL"
    End With

    Dim adapter As New SqlDataAdapter(comm)
    adapter.Fill(dt)                    'Fill DT with Query results

    'read through dataset and write records that meet date criteria to array
    Dim aEmpList(dt.Rows.Count, 5) As String
    Try
        lCount = 0
        For i As Integer = 0 To dt.Rows.Count - 1
            If clsEncrypt.DecryptData(dt.Rows(i)(3)) >= 20130101 Then
                aEmpList(lCount, 0) = clsEncrypt.DecryptData(dt.Rows(i)(0))
                aEmpList(lCount, 1) = clsEncrypt.DecryptData(dt.Rows(i)(1))
                aEmpList(lCount, 2) = clsEncrypt.DecryptData(dt.Rows(i)(2))
                aEmpList(lCount, 3) = clsEncrypt.DecryptData(dt.Rows(i)(3))
                aEmpList(lCount, 4) = clsEncrypt.DecryptData(dt.Rows(i)(4))
                lCount = lCount + 1
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(e.ToString())
    End Try

    'populate structure with aEmpList array
    Dim myarr(dt.Rows.Count) As mystructure
    Try
        For i As Integer = 0 To lCount - 1
            myarr(i) = New mystructure With {.Dim1 = aEmpList(i, 0).ToString, .Dim2 = aEmpList(i, 1).ToString, .Dim3 = aEmpList(i, 2).ToString, .Dim4 = aEmpList(i, 3).ToString, .Dim5 = aEmpList(i, 4).ToString}
        Next
    Catch ex As Exception
        MessageBox.Show(e.ToString())
    End Try

    'use myarr structure as source for datagridview
    DataGridView1.DataSource = myarr      'fill DGV

It's lengthy, and kind of messy, but it works.

In sum, I have encrypted fields within a SQL Server 2008 R2 database. I am trying to bring back records using a SELECT * FROM Table WHERE DATE >= 20130101 (just for examples' sake). Well, when the date fields are encrypted, it's not possible to pass a WHERE clause and return data. So I had to bring it back, decrypt it, and store it in a data table. I then created an array aEmpList, and filled it with the decrypted data fields. I finally loaded the structure with datatable decrypted aEmpList array and used it as the DataSource for a DGV.

Hoepfully someone can find this useful.

OTHER TIPS

What you can do is query your datatable after you get the data from the server and it has been decrypted using something like this:

Dim DRs as DataRow() = dt.Select("BEGINDATE >= 12/21/2013")

There are lots of other ways too, like using Linq, but since I dont know what version of VB you are using, this works in all versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top