如何修复无法正常工作的SQL语句?

时间:2013-07-30 13:25:12

标签: sql vb.net winforms

更新

winform有一个datetimepicker 4个常规texboxes和2个蒙版texbox以及一个绑定的datagridview。

我正在使用datagridview的cellformatting事件从另一个数据表中查找员工的姓名。为了实现这一点,我使用的是一个大部分工作的sql语句。

就像现在一样,它返回第二个数据表中不同列的员工姓名,并将其设置在数据网格视图的第二列中,紧跟在员工ID之后。

不工作的部分正在查看终止日期(“FSalida”列),并且只有在没有终止日期或所选日期早于终止日期时才返回员工姓名。换句话说,如果雇员已被终止,则不应在终止日期之后出现。

我的代码是:

 Private Sub PartePersonalDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    'This looks up the employee's full name using the employee number from the ID column (first column) 
    Try
        Dim dgvr As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
        If dgvr.Cells(0).Value IsNot Nothing AndAlso dgvr.Cells(0).Value IsNot DBNull.Value Then
            Dim empID As Integer = CInt(dgvr.Cells(0).Value)
            Dim qry = From dr As PersonalObraDataSet.PersonalObRow In PersonalObraDataSet.PersonalOb
            Where (dr.cdTrabajador = empID) And (dr.FSalida = Nothing) Or (dr.FSalida <= txtDate.Text)
            'This returns each part of the name and joins it in the the 2nd column (name column)
            DataGridView1.Rows(e.RowIndex).Cells(1).Value = (qry.First.Nombre1 & " " & qry.First.Nombre2 & " " & qry.First.Apellido1 & " " & qry.First.Apellido2)
            DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = DefaultBackColor
        End If
        'If there is an exemption like the employee doesn't exists this turns the background color red and instead
        'of the name on the second column it shows "employee doesn't exist."
    Catch ex As Exception
        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
        DataGridView1.Rows(e.RowIndex).Cells(1).Value = "ESTE EMPLEADO NO EXISTE"
        Exit Sub

不工作的部分是在“And”之后:

Where (dr.cdTrabajador = empID) And (dr.FSalida = Nothing) Or (dr.FSalida <= txtDate.Text)

这是查找表中数据的示例

ProjectID   EmployeeID  DepartmentID    HIreDate    TerminationDate     Name    Middle  Last
3116        83               1      09/03/2012      27/04/2012          John        Doe     Doe2
3116        373              1      16/11/2012                          Pedro    John       Ortiz
3116        1                1      01/01/2013                          Jose      Maria   Applessed

非常感谢你帮助解决这个问题。

1 个答案:

答案 0 :(得分:0)

您已经获得了上述几行解决方案,使用DBNull.Value代替Nothing

Where (dr.cdTrabajador = empID) And (dr.FSalida = DBNull.Value) Or (dr.FSalida <= txtDate.Text)

或者,您可以在FSalida的构造函数中将DateTime.MinValue字段设置为PersonalObRow(我相信这是DateTime的默认值)并检查,例如:

Class PersonalObRow

Public FSalida As DateTime 'the VB Date keyword is synonymous to System.DateTime

    Public Sub New()

        FSalida = DateTime.MinValue

    End Sub

End Class

或者您可以将其设为Nullable(Of DateTime),这样您就可以检查它是否为Nothing,例如:

Class PersonalObRow

    Public FSalida As Nullable(Of DateTime) 'or Nullable(Of Date)

End Class