简体   繁体   English

VB:格式化Mysql时间戳

[英]VB: Formatting Mysql Timestamp

I don't know how to format a timestamp in VB so it gets inserted into the MYSQL database in this format:我不知道如何在 VB 中格式化时间戳,因此它以这种格式插入 MYSQL 数据库:

YYYY-MM-DD 00:00:00

I tried:我试过了:

Dim timestamp As Date = TimeValue(Now)

It formats it as 0001-01-01 12:33:38 -- This would be correct if the yyyy-mm-dd was actually the correct date.它将其格式化为0001-01-01 12:33:38 -- 如果yyyy-mm-dd实际上是正确的日期,这将是正确的。

You missed the point.你错过了重点。

Real timestamps in sql databases are never formatted YYYY-MM-DD 00:00:00 . sql 数据库中的实际时间戳永远不会格式化YYYY-MM-DD 00:00:00 That's just how they are displayed in a query or data window.这就是它们在查询或数据 window 中的显示方式。 These values are actually stored in a binary format that is often not human readable, though mysql might still use unix time.这些值实际上以通常不可读的二进制格式存储,尽管 mysql 可能仍使用 unix 时间。

The point is that you don't even need to know for certain what the actual format is.关键是您甚至不需要确定实际格式是什么。 This should be handled by your database connection provider, rather than formatting it yourself.这应该由您的数据库连接提供商处理,而不是自己格式化。 Inserting/updating a database timestamp correctly for mysql from VB.Net should look something like this:从 VB.Net 为 mysql 正确插入/更新数据库时间戳应该如下所示:

Dim sql As String = "UPDATE `Table` Set MyColumn= @DateValue WHERE ID= @ID"
Using cn As New MySqlConnection("Your connection string here"), _
      cmd As New MySqlCommand(sql, cn)

    cmd.Parameters.Add("@DateValue", MySqlDbType.Datetime).Value = Now
    cmd.Parameters.Add("@ID", MySqlDbType.Int32).Value = 12345

    cn.Open()
    cmd.ExecuteNonQuery()
End Using

No formatting necessary.无需格式化。 You could also just write the query this way:你也可以这样写查询:

Dim sql As String = "UPDATE `Table` Set MyColumn= current_timestamp WHERE ID= @ID"

But if you really insist on creating the string, it would look like this:但如果你真的坚持要创建字符串,它看起来像这样:

Now().ToString("yyyy-MM-dd HH:mm:ss")

using the format string instructions available here:使用此处提供的格式字符串说明:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

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

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