Sql server 如何使用VB.NET中的语句从SQL Server数据库中获取一周列表?
我想知道是否可以设置这个 这是来自SQL Server的 我的逻辑解释是 VB.NET:Sql server 如何使用VB.NET中的语句从SQL Server数据库中获取一周列表?,sql-server,vb.net,Sql Server,Vb.net,我想知道是否可以设置这个 这是来自SQL Server的 我的逻辑解释是 VB.NET: If "Date_Needed between `getdate()` And `dateadd(day,7,getdate())` " Then `DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red` End If 或 Date\u needed是从SQL Server获取的列值 如果系统读取日期为一周的datagridview
If "Date_Needed between `getdate()` And `dateadd(day,7,getdate())` "
Then
`DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red`
End If
或
Date\u needed
是从SQL Server获取的列值
如果系统读取日期为一周的datagridview列表,datagridview中的选定行将变为红色forecolor高亮显示
Select *
From PRF_Form
Where Date_Needed Between getdate() And Dateadd(day, 7, getdate())
我会在返回的结果集中添加一列,以指示该行是否属于“NeededDate”选择。让我的数据库在那里完成繁重的工作,只需依靠VB.NET端根据查询返回的值进行样式设置 但是,首先,我想指出你在回答这个问题时提出的一个问题。VB.NET中的“&”是字符串连接运算符,而不是逻辑“与”运算使用时将对象隐式转换为字符串 所以,这句话在你的回答中
Date.Now & DateAdd(intervaltype, days, secondDate)
将生成类似以下内容的字符串:2019年4月13日至52019年4月13日
这不利于日期比较。此外,它不会抛出编译器错误,因为它会自动将compare运算符左侧的date对象转换为string,因为右侧的值是string
前面已经说过了,但是您确实需要实际日期类型中的所有元素来比较日期
所以试试这样吧
Dim startdate As Date
If Not Date.TryParse(DateString, startDate) Then startDate = Date.Now
Dim days As Double = 7
Dim secondDate As Date
secondDate = DateAdd(DateInterval.Day, days, startdate)
For i = 0 To dt.Rows.Count - 1
Dim checkDate As Date = Date.Parse(dt.Rows(i)(5).ToString)
If checkDate <= Date.Now AndAlso checkDate >= secondDate Then
DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red
End If
Next
Dim startdate作为日期
如果不是Date.TryParse(DateString,startDate),则startDate=Date.Now
双倍暗日=7
将第二个日期变为日期
secondDate=DateAdd(DateInterval.Day,days,startdate)
对于i=0到dt.Rows.Count-1
Dim checkDate As Date=Date.Parse(dt.Rows(i)(5.ToString)
如果checkDate=secondDate,则
DataGridView1.Rows(i).DefaultCellStyle.ForeColor=Color.Red
如果结束
下一个
请注意“AndAlso”运算符。这类似于
和运算符,但仅当左侧的比较运算符返回true时,才会检查其右侧的比较选项,并且也返回true。我会在返回的结果集中添加一列,以指示行是否属于“NeededDate”选择。让我的数据库在那里完成繁重的工作,只需依靠VB.NET端根据查询返回的值进行样式设置
但是,首先,我想指出你在回答这个问题时提出的一个问题。VB.NET中的“&”是字符串连接运算符,而不是逻辑“与”运算使用时将对象隐式转换为字符串
所以,这句话在你的回答中
Date.Now & DateAdd(intervaltype, days, secondDate)
将生成类似以下内容的字符串:2019年4月13日至52019年4月13日
这不利于日期比较。此外,它不会抛出编译器错误,因为它会自动将compare运算符左侧的date对象转换为string,因为右侧的值是string
前面已经说过了,但是您确实需要实际日期类型中的所有元素来比较日期
所以试试这样吧
Dim startdate As Date
If Not Date.TryParse(DateString, startDate) Then startDate = Date.Now
Dim days As Double = 7
Dim secondDate As Date
secondDate = DateAdd(DateInterval.Day, days, startdate)
For i = 0 To dt.Rows.Count - 1
Dim checkDate As Date = Date.Parse(dt.Rows(i)(5).ToString)
If checkDate <= Date.Now AndAlso checkDate >= secondDate Then
DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red
End If
Next
Dim startdate作为日期
如果不是Date.TryParse(DateString,startDate),则startDate=Date.Now
双倍暗日=7
将第二个日期变为日期
secondDate=DateAdd(DateInterval.Day,days,startdate)
对于i=0到dt.Rows.Count-1
Dim checkDate As Date=Date.Parse(dt.Rows(i)(5.ToString)
如果checkDate=secondDate,则
DataGridView1.Rows(i).DefaultCellStyle.ForeColor=Color.Red
如果结束
下一个
请注意“AndAlso”运算符。这类似于和
运算符,但仅当左侧的比较运算符返回true时,才会检查其右侧的比较选项。我想代码应该是这样的?对于i=0到dt.Rows.Count-1,如果dt.Rows(i)(9).ToString=Date.Now和DateAdd(DateInterval.Day,7),那么DataGridView1.Rows(i).DefaultCellStyle.ForeColor=Color.Red End如果下一步代码应该是这样的,我想?对于i=0到dt.Rows.Count-1,如果dt.Rows(i)(9)。ToString=Date.Now和DateAdd(DateInterval.Day,7),则DataGridView1.Rows(i)。DefaultCellStyle.ForeColor=Color。如果下一步也尝试此选项,则红色结束,但不会发生任何情况。在死亡线前一周没有红色亮点我也尝试了这一次,但什么都没有发生。截止日期前一周没有红色突出显示