在datagridview上的时间 [英] time on datagridview
本文介绍了在datagridview上的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个按钮,当我点击它时,它会记录一个数据来访问表格,我可以在datagridview上查看它。我把日期和时间表分开了。所以它显示了具有正确日期的日期列。但是时间列显示为1/1/2001 6:39:56 pm。我在访问桌上使用长格式时间
我的代码有什么问题吗?
i'm making a button that when i click, it record a data to access table and i can view it on datagridview. i have the table of date and time separated. so it shown date column with the right date. but the time column shown 1/1/2001 6:39:56pm. i'm using long format for time at access table
is there anything wrong with my code?
Sub catat()
sql = "insert into [userhistory] ([username],[date recorded],[time recorded],[activity],[reg number]) values (?,?,?,?,?)"
kns.Open()
Using cmd As New OleDbCommand(sql, kns)
cmd.Parameters.AddWithValue("@p1", LOGIN.USERNAME.Text)
cmd.Parameters.AddWithValue("@p2", System.DateTime.Today.ToString)
cmd.Parameters.AddWithValue("@p3", TimeOfDay.ToString)
cmd.Parameters.AddWithValue("@p4", "Deleting User")
cmd.Parameters.AddWithValue("@p5", dg.CurrentRow.Cells(0).Value.ToString)
cmd.ExecuteNonQuery()
End Using
kns.Close()
End Sub
推荐答案
请,不要将日期和时间存储为字符串 - 不要存储在任何数据库中!
相反,将日期和时间存储在DATETIME字段中,然后返回所需的信息,格式正确,为您的表格。
特别是基于字符串的日期是一个噩梦,因为它们将以一种格式存储取决于插入数据的计算机的设置:因此,如果设置更改,(例如,从更喜欢欧洲日期格式dd / MM / yy到美国格式MM / dd / yy或日语yy / MM / dd的用户)然后数据库中的数据变得不一致。
这会在问题发生后的相当长的一段时间内引发可怕的问题,并且修复数据可能是一场真正的噩梦。
存储为DATETIME值可以避免这一切。
因此更改数据库,更改为代码以反映那,然后看看格式化输出 - 这真的,真的,简单的位!
Please, don't store dates and times as strings - not in any database!
Instead, store a date and time together in a DATETIME field, and then return just the information you want, formatted correctly, for your table.
String based dates in particular are a nightmare to work with, because they will be stored in a format which depends on the settings for the computer that inserted the data: so if the settings change, (say from a user who prefers European date format dd/MM/yy to a US format MM/dd/yy or Japanese yy/MM/dd) then the data in your DB becomes inconsistent.
This causes horrible problems some considerable time after the problem occurred, and it can be a real nightmare to fix the data.
Storing as DATETIME values avoids all this.
So change your DB, change to code to reflect that, and then look at formatting the output - which is the really, really, easy bit!
这篇关于在datagridview上的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文