2014-03-04 2 views
1

날짜 필드가 "MUST"인 데이터베이스가 암호화되어 있습니다.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에있는 Column 19 & 20은 BeginDateEndDate입니다.

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%  [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을 생성하고 해독 된 데이터 필드로 채 웁니다. 나는 마지막으로 데이터 구조가 해독 된 aEmpList 배열을로드하고 DGV의 DataSource로 사용했습니다.

호기심 많은 누군가가 유용하다는 것을 알 수 있습니다.

1

당신이 할 수있는 일 서버에서 데이터를 가져온 후 데이터베이스를 쿼리하면 다음과 같은 내용으로 해독됩니다.

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

Linq를 사용하는 것과 같은 다른 많은 방법이 있지만 사용중인 VB의 버전을 알지 못하므로 모든 버전에서 작동합니다.

+0

안녕하세요, 스티브, 답변 해 주셔서 감사합니다.'DataGridView1'에 결과를 넣고 싶다면'DataRow' 코드를 어떻게 활용하면 될까요? (나는 그것이 또 다른 질문 인 것을 안다. 그러나 그것이 여전히 적절하다고 느낀다.) –

관련 문제