代码之家  ›  专栏  ›  技术社区  ›  jamesaharvey

.Net中MySQL数据库的日期时间格式

  •  1
  • jamesaharvey  · 技术社区  · 15 年前

    我尝试了以下方法,但没有成功:

    insert blah 
      (Id, Content, DateCreated) 
      select 123, 'Blah blah blah', 1/5/2010 9:04:58 PM
    
    insert blah 
      (Id, Content, DateCreated) 
      select 123, 'Blah blah blah', '1/5/2010 9:04:58 PM'
    
    2 回复  |  直到 15 年前
        1
  •  5
  •   Thomas Levesque    15 年前

    不要在查询中输入文字日期,而是使用参数。这样你就不用担心格式了。对于用户输入的字符串也更安全,因为它可以防止SQL注入。

    command.Text = "insert into myTable(myDate) values(?dateParam)";
    command.Parameters.Add("?dateParam", theDate);
    
        2
  •  1
  •   Randolpho    15 年前

    MySqlCommand 而不是构建自己的查询并将DateTime作为参数传递给。不需要格式化。