簡體   English   中英

VB.Net - 如何讓 GridView 只顯示 DateTime SQL 列中的日期?

[英]VB.Net - How to get GridView to only show Date from DateTime SQL column?

我有一個 SQL 公式,我計划在即將在 VB.NET 中開發的 Web 程序中使用該公式,該程序將在 GridView 中顯示從我的 SQL 公式獲得的數據。

我的 SQL 的某個特定部分遇到了問題

select A.[Inv No], A.[Project No], 
cast([Record Date] as date)as [Date],   --This area is problem field
A.[Description], A.[Problem + Repair Details], 
A.[Status], convert(float, A.[Accumulative Stroke]) 
as [Accumulative Stroke],
convert(float, A.[Preventive Stroke]) 
as [Preventive Stroke], 
A.[PIC], A.[Measurement (OK/NG)] 
from [SQL].[dbo].[MAINT_ENTRY] A 

left join (select [Inv No], max(Date +' '+ Time) as [Record Date] 
from [SQL].[dbo].[MAINT_ENTRY] 
group by [Inv No]) B 

on A.[Inv No] = B.[Inv No]

where [Record Date] = Date +' '+ Time and 
[Problem + Repair Details] <> '""' 
Order by A.[Status], A.[Inv No], A.[Date]desc

[Record Date]是將DateTime列合並為單個合並列的列,以便我獲得最新數據以顯示在我的程序中。 這兩個字段都需要采用datetime格式才能加入。

我希望 GridView 只在Record Date顯示 Date ,並且由於它在datetime我嘗試使用CAST將格式更改為date

雖然它在 SQL Management Studio 中顯示了正確的結果,我在其中模擬了結果,但它似乎不想在 VB.Net 中很好地發揮作用,因為我得到的結果仍然使用datetime格式。 它會刪除時間數據,但仍保留時間格式。

22/4/2019 08:45:00 AM ---> 22/4/2019 12:00:00 AM

似乎是什么問題導致了這個?

您正在嘗試直接在 SQL 查詢中強制轉換日期時間,以便這樣做而不是

cast([Record Date] as date)as [Date]

你應該這樣做:

CONVERT(varchar(10), [Record Date], 103) as [Date]

正如CONVERT ( data_type [ ( length ) ] , expression [ , style ] )語法指定的那樣: 參考

如果我理解你的問題,你是從一些具有 DateTime 字段的 SQL 服務器返回一個對象。 您想使用 VB 僅顯示日期組件。

在 VB 中,DateTime 對象有一個“Date”屬性,您可以使用它來僅顯示 DateTime 屬性的日期組件。 一個簡單的代碼示例:

Dim myDateTime as DateTime = now()
MessageBox.Show("Today's date is: " + myDateTime.Date)

如果您需要更多參考,請查看https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM