从vb.net中的访问数据库检索数据到数据网格视图

时间:2013-05-01 07:00:32

标签: vb.net ms-access datagridview datetimepicker

我尝试从具有特定日期的访问数据库中读取数据到VB.NET中的datagrid视图。我使用日期时间选择器。下面的代码我用于retreive数据。但我按下查找按钮。 datagrid视图中没有显示任何内容。这是代码

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 个答案:

答案 0 :(得分:1)

当你构建一个连接字符串的sql命令时,你会将代码暴露给两个大问题。

  • 首先,您可以在a上为特定数据类型编写错误的语法 特定数据库
  • 其次,您将代码暴露给来自a的Sql Injection攻击 恶意用户(好吧,这里不应该是一个问题,但....)

而是构建一个参数化查询,让框架代码以安全和正确的方式与数据库引擎交互

所以你应该写:

    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的值时出现另一个潜在错误。从您的代码看,该字段似乎是decimal或double类型。如果您的代码编译,那么您可能已将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