设置NULL日期

时间:2013-11-27 15:32:14

标签: sql vb.net entity-framework

我的数据库中有一个日期字段,当用户单击按钮取消激活时,客户设置为非活动状态时设置该日期字段。当用户试图反应客户时,我无法弄清楚如何清除非活动日期。

这是停用按钮代码的样子:

Private Sub btnDeactivate_Click(sender As System.Object, e As EventArgs) Handles btnDeactivate.Click
        Dim selectedCustomers = GetSelectedCustomers()
        Dim newInActiveFlag = Not selectedCustomers.Any(Function(c) (c.Inactive_Flag = "Y"))
        Dim activateWord = "Activate"
        If newInActiveFlag Then
            activateWord = "Deactivate"
        End If

        Dim result As DialogResult = MessageBox.Show(String.Format("Are you sure you want to {0} this Customer?", activateWord.ToLower()), activateWord + " Customer", _
                                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = Windows.Forms.DialogResult.Yes Then

            Dim selectedCustomerIds = selectedCustomers.Select(Function(c) (c.Cust_Num))

            Dim service = New DataService()

            service.Customers.SetInactiveFlags(selectedCustomerIds, newInActiveFlag)
            service.SaveChanges()
            PerformSearch(_lastSearchTxt, _lastIncludeActive)
        End If
    End Sub

这是设置非活动标志和日期的部分:

    Public Sub SetInactiveFlags(custIds As IEnumerable(Of Integer), inactiveFlag As Boolean)
        Dim customers As List(Of Customer)
        customers = _context.Customers.Where(Function(c) (custIds.Contains(c.Cust_Num))).ToList()

        For Each customer As Customer In customers
            customer.Inactive_Flag = CreateFromBoolean(inactiveFlag)
            customer.Inactive_Date = DateTime.Today
        Next

    End Sub

是否可以设置相同的按钮,并按照我设置的相同方式删除日期并删除非活动标志?

感谢

2 个答案:

答案 0 :(得分:0)

假设您在null字段上允许Inactive_Date个值,您只需将其设置为Nothing即可。如果您将SetInactiveFlags方法更改为具有Nullable(Of DateTime)参数(或使用DateTime快捷方式?ala C#),则可以直接传递它:

Public Sub SetInactiveFlags(custIds As IEnumerable(Of Integer), inactiveDate As DateTime?, inactiveFlag As Boolean)
...
customer.Inactive_Date = inactiveDate
...

然后您可以使用SetInactiveFlags(selectedCustomerIds, Nothing, newInActiveFlag)SetInactiveFlags(selectedCustomerIds, DateTime.Now, newInActiveFlag)

实体框架/ Linq2Sql会自动将Nothing转换为DBNull.Value。

答案 1 :(得分:0)

好吧我觉得我找到了解决方案,我接受了汤姆的建议并将其添加到了btn点击事件中:

If activateWord = "Deactivate" Then
                service.Customers.SetInactiveFlags(selectedCustomerIds, DateTime.Today, newInActiveFlag)
            ElseIf activateWord = "Activate" Then
                service.Customers.SetInactiveFlags(selectedCustomerIds, Nothing, newInActiveFlag)
            End If

所以整篇文章看起来像这样:

    Private Sub btnDeactivate_Click(sender As System.Object, e As EventArgs) Handles btnDeactivate.Click
        Dim selectedCustomers = GetSelectedCustomers()
        Dim newInActiveFlag = Not selectedCustomers.Any(Function(c) (c.Inactive_Flag = "Y"))
        Dim activateWord = "Activate"
        If newInActiveFlag Then
            activateWord = "Deactivate"
        End If

        Dim result As DialogResult = MessageBox.Show(String.Format("Are you sure you want to {0} this Customer?", activateWord.ToLower()), activateWord + " Customer", _
                                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = Windows.Forms.DialogResult.Yes Then

            Dim selectedCustomerIds = selectedCustomers.Select(Function(c) (c.Cust_Num))

            Dim service = New DataService()
            If activateWord = "Deactivate" Then
                service.Customers.SetInactiveFlags(selectedCustomerIds, DateTime.Today, newInActiveFlag)
            ElseIf activateWord = "Activate" Then
                service.Customers.SetInactiveFlags(selectedCustomerIds, Nothing, newInActiveFlag)
            End If
            service.SaveChanges()
            PerformSearch(_lastSearchTxt, _lastIncludeActive)
        End If
    End Sub

我还将SetInactiveFlags更改为:

    Public Sub SetInactiveFlags(custIds As IEnumerable(Of Integer), Inactive_Date As DateTime?, inactiveFlag As Boolean)
        Dim customers As List(Of Customer)
        customers = _context.Customers.Where(Function(c) (custIds.Contains(c.Cust_Num))).ToList()

        For Each customer As Customer In customers
            customer.Inactive_Flag = CreateFromBoolean(inactiveFlag)
            customer.Inactive_Date = Inactive_Date
        Next

    End Sub

我花了一分钟时间了解逻辑,但感谢您花时间帮助我,我真的很感激。