简体   繁体   English

如何在VB.net中比较和SQL日期

[英]How to compare and SQL date in VB.net

I have a SQL Server database and what I want to do is compare a start date ( date ) and an end date ( date ) that I stored in my database on a row like this: 我有一个SQL Server数据库,我想要做的是比较我在数据库中存储的开始日期( date )和结束日期( date ),如下所示:

  • Column1 - Row1 (start date) = 16-03-2016 Column1 - Row1(开始日期)= 16-03-2016
  • Column2 - Row1 (end date) = 16-03-2017 第2栏 - 第1行(结束日期)= 16-03-2017

Both of these dates must be checked with the current date inside a sub in vb.net and I did this: 必须使用vb.net中sub的当前日期检查这两个日期,我这样做了:

If RecieverData.HasRows Then
    While RecieverData.Read
        Label1Recieverinfo.Text = "Klant: " & RecieverData("Ontvanger").ToString
        Label2Recieverinfo.Text = "ID: " & RecieverData("OntvangerID").ToString
        Label3Recieverinfo.Text = "Event: " & RecieverData("event").ToString
        Label4Recieverinfo.Text = "Startdatum: " & RecieverData("Startdate").ToString
        Label5Recieverinfo.Text = "Einddatum: " & RecieverData("Enddate").ToString
    End While

    Dim StartDate As New Date
    Dim EndDate As New Date

    StartDate = RecieverData("StartDate")
    EndDate = RecieverData("EndDate")

    If StartDate <= Date.Now Then
        MessageBox.Show("startdate ok")
    ElseIf EndDate >= Date.Now Then
        MessageBox.Show("Enddate ok")
    Else
        MessageBox.Show("Use me")
    End If

Else
    MessageBox.Show("Deze Ontvanger ID bestaat niet.", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If

But (you already knew this) it doesn't work. 但是(你已经知道了)它不起作用。

Can someone help me out here? 有人可以帮帮我吗?

Thanks 谢谢

Edit: 编辑:

Label results: 标签结果:

在此输入图像描述

Error that I get: 我得到的错误:

在此输入图像描述

Your while loop is reading past the end of the data. 您的while循环正在读取数据的末尾。 If you only expect one row, you should just read once: 如果你只想要一行,你应该只阅读一次:

If RecieverData.HasRows Then

    ' Read the first row
    RecieverData.Read()

    Label1Recieverinfo.Text = "Klant: " & RecieverData("Ontvanger").ToString
    Label2Recieverinfo.Text = "ID: " & RecieverData("OntvangerID").ToString
    Label3Recieverinfo.Text = "Event: " & RecieverData("event").ToString
    Label4Recieverinfo.Text = "Startdatum: " & RecieverData("Startdate").ToString
    Label5Recieverinfo.Text = "Einddatum: " & RecieverData("Enddate").ToString

    Dim StartDate As New Date
    Dim EndDate As New Date

    StartDate = RecieverData("StartDate")
    EndDate = RecieverData("EndDate")

    If StartDate <= Date.Now Then
        MessageBox.Show("startdate ok")
    ElseIf EndDate >= Date.Now Then
        MessageBox.Show("Enddate ok")
    Else
        MessageBox.Show("Use me")
    End If

Else
    MessageBox.Show("Deze Ontvanger ID bestaat niet.", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM