我有一个数据库,其日期字段“必须”被加密。
要直接从数据库解密,我使用:
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,第19栏和第19栏中。 20是BeginDate
和EndDate
。
如果我需要撤回查询,例如SELECT EMPL_FIRST_NM, EMPL_LAST_NM FROM ???? WHERE BEGINDATE >= 12/21/2013
,我需要查看解密的日期值。
我见过类似的东西:
Dim dr As DataRow() dr =
但我不确定我的具体情况。
为了更好的视觉效果: 在我填充我的DGV的DataTable中(省略了一些行)
+-----------------------------------------------+
| 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% $@GFgeh $%@YYWEG |
| K&^D@ 54F#$3 L:er@# %$@YG&^ NH#%HJBR |
| D!@#$ RGER454 M$#Rz $%T@GERG hYE76F& |
| vfbDW[ DQWR5rf ~gE5yb #$!TDDg mHY6$1* |
+------------------------------------------------+
答案 0 :(得分:1)
您可以做的是从服务器获取数据后查询数据表,并使用以下内容解密数据:
Dim DRs as DataRow() = dt.Select("BEGINDATE >= 12/21/2013")
还有许多其他方法,比如使用Linq,但由于我不知道你使用的是什么版本的VB,这适用于所有版本。
答案 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
,并用解密的数据字段填充它。我最终加载了带有数据表解密aEmpList
数组的结构,并将其用作DGV的数据源。
有人可以发现这很有用。