我想从数据库中选择未来15天的数据 [英] I want to select data from of next coming 15 days from database

查看:62
本文介绍了我想从数据库中选择未来15天的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作门通过期的应用程序

所以在主要表格中我放了一个数据网格视图,数据网格视图将显示未来1个月到期门通行证的数据



i写下面的代码,但它显示了未来2个月即将到期的门票清单





请帮助



我的尝试:



私有子MDIParent1_Load(发件人作为对象,e作为EventArgs)处理MyBase.Load

expiryalert()

结束子

Public Sub expiryalert()

尝试

Dim da As New SqlDataAdapter

Dim ds As New DataSet

Dim cmd作为SqlCommand

connection()

cmd =新的SqlCommand(select * from gatepassdetails where expiry>'& Format(Today.AddMonths(1),MM) / dd / yyyy)&',connsql)

d a.SelectCommand = cmd

da.Fill(ds,gatepassdetails)

data_expired.DataSource = ds

data_expired.DataMember =gatepassdetails

Catch ex As Exception

MsgBox(ex.Message.ToString)

connsql.Close()

结束尝试

结束Sub

i am making an app for gate pass expiry
so in main form i put one data grid view the data grid view will show the data of expiring gate passes in next 1 month

i write the below code but it is showing the list of gate passes which are going to expire in next 2 months also


please help

What I have tried:

Private Sub MDIParent1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
expiryalert()
End Sub
Public Sub expiryalert()
Try
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd As SqlCommand
connection()
cmd = New SqlCommand("select * from gatepassdetails where expiry > '" & Format(Today.AddMonths(1), "MM/dd/yyyy") & "'", connsql)
da.SelectCommand = cmd
da.Fill(ds, "gatepassdetails")
data_expired.DataSource = ds
data_expired.DataMember = "gatepassdetails"
Catch ex As Exception
MsgBox(ex.Message.ToString)
connsql.Close()
End Try
End Sub

推荐答案

正如评论中所提到的,你真正想要使用的是小于。如果您将其翻译成英语,那么您目前所拥有的是到期日期晚于一个月。你真正想要的是从现在起不到一个月的时间。我不确定您的系统是否需要,但您可能希望包含并且大于或等于今天的日期。
As mentioned in the comments, what you really want to use is less than. If you translate it into English, what you currently have is where the expiry date is later than a month from now. What you really want is what is less than a month from now. I'm not sure if your system needs it or not but you may want to include and greater than or equal to today's date.


而不是使用AddMonths和Format(加上字符串连接)考虑使用DATEADD:

Rather than using AddMonths and Format (plus a string concatenation) consider using DATEADD:
cmd = New SqlCommand("SELECT * FROM gatepassdetails WHERE expiry < DATEADD(mm, 1, GETDATE())", connsql)

保留它所有内部SQL。

Which keeps it all inside SQL.


这篇关于我想从数据库中选择未来15天的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆