2014-03-04 50 views
1

我有一個數據庫,其日期字段「必須」被加密。將數據加載到DataTable中並解密,然後對其執行查詢

要直接從數據庫解密,我用這個:

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 

我的情況:我需要運行鍼對特定日期範圍WHERE條款。因此,目前在我的DGV中,列BeginDateEndDate

如果我需要撤回一個查詢,如SELECT EMPL_FIRST_NM, EMPL_LAST_NM FROM ???? WHERE BEGINDATE >= 12/21/2013我需要查看解密的日期值。

我所看到的是這樣的:

Dim dr As DataRow() dr = 

但我不知道我的具體情況。

爲了更好的視覺享受: 在我填充我DGV有(某些行略)的數據表

+-----------------------------------------------+ 
| 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| 
+-----------------------------------------------+ 

是什麼樣子在DB:

+------------------------------------------------+ 
| EMP_ID EMP_F_NAME EMP_L_NAME BEG_DT END_DT | 
+------------------------------------------------+ 
| ^##$D  @3sAdfq MR%  [email protected] $%@YYWEG | 
| K&^[email protected]  54F#$3 L:[email protected]# %[email protected]&^ NH#%HJBR | 
| [email protected]#$  RGER454 M$#Rz $%[email protected] hYE76F& | 
| vfbDW[  DQWR5rf ~gE5yb #$!TDDg mHY6$1* | 
+------------------------------------------------+ 

回答

0

所以,我不確定DataRow方法,因爲我不確定如何使用它。這部分是我的錯,因爲我忘記在我的OP中提到我需要真正能夠使用這些數據,無論是報告還是顯示DGV中的數據。

這就是說,我發現代碼,導致我使用Structure。我以前從來沒有用過,所以這可能是完全錯誤的 - 但它是有效的。

'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 

現在,我們有結構準備好接受我們進入它的價值..

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 

這是漫長的,而那種凌亂,但很有效。

總之,我在SQL Server 2008 R2數據庫中加密了字段。我正在嘗試使用SELECT * FROM Table WHERE DATE >= 20130101(僅僅爲了舉例)帶回記錄。那麼,當日期字段被加密時,不可能通過WHERE子句並返回數據。所以我不得不把它拿回來,解密它,並將它存儲在數據表中。然後我創建了一個數組aEmpList,並用解密的數據字段填充它。我終於裝載了數據表解密的數組,並將其用作DGV的DataSource。

很高興有人可以找到這個有用的。

1

你可以做什麼在從服務器獲取數據並且已經使用類似如下內容解密的情況下查詢您的數據表:

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

還有很多其他的方法,比如使用Linq,但是因爲我不知道你使用的是什麼版本的VB,所以在所有版本中都適用。

+0

嘿史蒂夫,謝謝你的回答!如果我想將結果放入'DataGridView1',我的代碼將如何改變,利用'DataRow'代碼? (我知道這是另一個問題,但我覺得它仍然相關。) –

相關問題