【问题标题】:Try to insert date by C# into .accdb but the data went wrong尝试通过 C# 将日期插入 .accdb 但数据出错
【发布时间】:2021-09-06 15:34:57
【问题描述】:

我试图运行这个 sql

insert into Registration (sn, regDate) values ('RS5044737',Format('2021/06/15 10:22:20', 'yyyy/mm/dd hh:nn:ss'));

但在我的 .accdb 文件中,数据出错如下:

我不知道为什么年份会变成 1478 年......

环境:

C# 2008

访问 2007

【问题讨论】:

  • 顺便说一句,2008 年是 VS 的特别史前版本 - 为什么不升级到 2019 年?这是一种更好的开发体验
  • 这个问题是由一个十年前开发的程序导致的,更新已停止。它已在许多国家/地区使用多年,直到最近有一个新客户加入,这个问题才发生过。
  • 所以,它错了 10 年 :) ... 这个问题从未发生过 - 错了这么久并不意味着它只是造成了一个问题,它仅仅意味着它对愿意报告它的人造成了问题。也并不意味着它不需要修复 - 做得很好,你投入了某事 :)

标签: c# sql ms-access


【解决方案1】:

如果你使用参数就不会发生

insert into Registration (sn, regDate) values (?, ?);

command.Parameters.AddWithValue("n", "RS5044737");
command.Parameters.AddWithValue("d", new DateTime(2021,6,15,10,22,20)); 

以相同的顺序添加参数?出现在语句中。

始终,始终使用参数。它可以防止:

  • 您遇到的数据错误
  • 如果您尝试在人名等中插入撇号,则会发生随机崩溃
  • 你因为导致massive breaches of security而被解雇

如果升级到 SQLserver,请不要使用 AddWithValue(但始终使用参数)

【讨论】:

    【解决方案2】:

    大写的MM是月份格式。 小写的 mm 是分钟。

    插入注册(sn,regDate)值('RS5044737',Format('2021/06/15 10:22:20', 'yyyy/MM/dd hh: mm:ss'));

    【讨论】:

      【解决方案3】:

      在我看来,你的格式字符串是错误的。

      我用来制作 SQL 日期和时间字符串的方法是:

      /// <summary>
      /// Formats a date time value so that it fits the JET specification
      /// for date formats to be usable from within a SQL query.
      /// The format is #MM/DD/YYYY HH:MM:SS# (including the hashs).
      /// </summary>
      public static string FormatJetDateAndTime(
          DateTime dateToFormat)
      {
          return string.Format(
              CultureInfo.CurrentCulture,
              @"#{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}#",
              dateToFormat.Month,
              dateToFormat.Day,
              dateToFormat.Year,
              dateToFormat.Hour,
              dateToFormat.Minute,
              dateToFormat.Second);
      }
      

      仅格式化日期,我使用这个:

      /// <summary>
      /// Formats the date portion of a date time value so that it fits into
      /// the JET specification for date formats to be usable from within a SQL query.
      /// The format is #MM/DD/YYYY# (including the hashs).
      /// </summary>
      public static string FormatJetDateOnly(
          DateTime dateToFormat)
      {
          return string.Format(
              CultureInfo.CurrentCulture,
              @"#{0:00}/{1:00}/{2}#",
              dateToFormat.Month,
              dateToFormat.Day,
              dateToFormat.Year);
      }
      

      为了格式化时间,我正在使用这个:

      /// <summary>
      /// Formats the time portion of a date time value so that it fits into
      /// the JET specification for date formats to be usable from within a SQL query.
      /// The format is #HH:MM:SS# (including the hashs).
      /// </summary>
      public static string FormatJetTimeOnly(
          DateTime dateToFormat)
      {
          return string.Format(
              CultureInfo.CurrentCulture,
              @"#{0:00}:{1:00}:{2:00}#",
              dateToFormat.Hour,
              dateToFormat.Minute,
              dateToFormat.Second);
      }
      

      当然,使用参数化查询作为Caius Jard points out 是最好的方法。

      【讨论】:

        【解决方案4】:

        要使用 Access SQL 插入固定值,请使用 octothorpes

        insert into Registration (sn, regDate) values ('RS5044737',#2021/06/15 10:22:20#);
        

        但是,很可能日期值不会固定,因此请使用 Caius 所示的参数 - 仅使用 AddValue,而不是 AddWithValue

        【讨论】:

        • Access 中插入语句的 AddWithValue 有什么问题?
        • @CaiusJard:你不熟悉其他语法。
        • 嗯.. 我明白这一点,但它不是一个强大的;老实说,我宁愿每个地方的每个人都用 AddWithValue 参数化所有东西,1% 的时间抱怨“我的 SQL Server 中的这个 select 语句像狗一样运行”,而不是没有参数化,99% 的时间都在抱怨“我的网站经常被脚本小子入侵”。 MySQL 甚至说“使用 AddWithValue”,所以我不会笼统地说“不要使用它,使用这种更难记住、更不方便的方式”的规则,因为人们只是......不会。让他们完全参数化将是一个更大的胜利
        • @CaiusJard:好的,可以同意。不知道 MySql 的推荐,谢谢。
        猜你喜欢
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2012-04-04
        相关资源
        最近更新 更多