2013-05-01 114 views
0

我嘗試用特定日期在VB.NET數據網格視圖來讀取訪問數據庫數據。我使用日期時間選擇器。以下代碼用於檢索數據。但我按查找按鈕。在datagrid視圖中沒有顯示。這是代碼檢索數據庫的訪問數據網格視圖數據在vb.net

Private Sub BTNFIND_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNFIND.Click 
    ATCEDITGRID.Rows.Clear() 
    getConnect() 
    'Dim editdate As String 
    DTPEDITAT.Value = Format(DTPEDITAT.Value, "dd/MM/yyyy") 
    'MessageBox.Show(DTPEDITAT.Value) 
    'editdate = DTPEDITAT.Value 
    Try 
     Conn.Open() 
     Dim strSQL As String = "SELECT EMP_ID,EMP_NAME,AT_STATUS,AT_REMARK FROM ATTENDANCE WHERE AT_DATE = " & DTPEDITAT.Value & " ORDER BY EMP_NAME ASC" 
     Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSQL, Conn) 
     Dim ds As DataSet = New DataSet 
     da.Fill(ds, "ATTENDANCE") 
     Dim dt As DataTable = ds.Tables("ATTENDANCE") 
     Dim row As DataRow 
     Dim atstat As String 
     For Each row In dt.Rows 
      If row("AT_STATUS") = 1 Then 
       atstat = "Present" 
      ElseIf row("AT_STATUS") = 0 Then 
       atstat = "Absent" 
      ElseIf row("AT_STATUS") = 0.5 Then 
       atstat = "Halfday" 
      Else 
       atstat = "Error" 
      End If 
      'MessageBox.Show(row("EMP_ID")) 
      'MessageBox.Show(row("EMP_NAME")) 
      'MessageBox.Show(atstat) 
      'MessageBox.Show(row("AT_REMARK")) 
      Me.ATCEDITGRID.Rows.Add(row("EMP_ID")) 
      Me.ATCEDITGRID.Rows.Add(row("EMP_NAME")) 
      Me.ATCEDITGRID.Rows.Add(atstat) 
      Me.ATCEDITGRID.Rows.Add(row("AT_REMARK")) 
     Next row 
     ATCEDITGRID.TopLeftHeaderCell.Value = "Sr.No." 
     Me.ATCEDITGRID.RowHeadersDefaultCellStyle.Padding = New Padding(3) 
     ATCEDITGRID.AllowUserToAddRows = False 
     AddRowHeadersEdit() 
     Conn.Close() 
    Catch ex As OleDb.OleDbException 
     MsgBox(ex.Message, MsgBoxStyle.Critical, "DB Error") 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error") 
    End Try 
End Sub 

請檢查代碼。並給我解決方案

回答

1

當你建立一個連接字符串的sql命令時,你的代碼暴露給兩個大問題。

  • 首先,你可以寫具體的數據類型錯誤的語法 特定數據庫上
  • 其次,你暴露你的代碼從一個 惡意用戶SQL注入攻擊(當然,這裏不應該是一個問題,但是......)

而是建立一個參數化查詢,讓框架代碼在安全和正確的方式

與數據庫引擎交互所以,你應該寫:

Conn.Open() 
    Dim strSQL As String = "SELECT EMP_ID,EMP_NAME,AT_STATUS,AT_REMARK " & _ 
          "FROM ATTENDANCE WHERE AT_DATE = ? ORDER BY EMP_NAME ASC" 
    Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSQL, Conn) 
    da.SelectCommand.Parameters.AddWithValue("@p1", DTPEDITAT.Value) 
    Dim ds As DataSet = New DataSet 
    da.Fill(ds, "ATTENDANCE") 

現在看看你的代碼,你可能會在處理字段AT_STATUS的值時出現另一個潛在的錯誤。從你的代碼看來,該字段是十進制或雙精度型的。如果您的代碼編譯,那麼您可能已將OPTION STRICT設置爲OFF。保持該選項爲ON並正確轉換數據類型是一種更好的做法。

Dim dt As DataTable = ds.Tables("ATTENDANCE") 
    Dim row As DataRow 
    Dim atstat As String 
    Dim status as Decimal 

    For Each row In dt.Rows 
     status = Convert.ToDecimal(row("AT_STATUS")) 
     If status = 1 Then 
      atstat = "Present" 
     ElseIf status = 0 Then 
      atstat = "Absent" 
     ElseIf status = 0.5 Then 
      atstat = "Halfday" 
     Else 
      atstat = "Error" 
     End If 
     ...... 
    Next row 
+0

@ Steve- thnx爲您的有價值的重播。 – Thanzeem 2013-05-01 08:02:19