【问题标题】:Add Date in Oracle Table for TimeStamp Column using c#使用 c# 在 Oracle 表中为时间戳列添加日期
【发布时间】:2019-11-18 14:42:23
【问题描述】:

我已编写代码以将日期添加到 oracle 表中,列类型为“TimeStamp” 但我收到了

的错误

'ORA-01843: not a valid month',我的代码在下面,oracle DB 中的列 'Timestamp' 类型,尝试使用 c# 更新列,

DateTime dt = 
        DateTime.Parse(Convert.ToString(CurrentItem[SharePointColumnInternal[j]]));
        dt = dt.ToLocalTime();
        UpdateCmd += i + "=" + "'" + dt + "'" + ",";  

这是我构建的更新命令字符串 :UpdateCmd 并在 c# 代码中执行。

【问题讨论】:

  • DateTime dt = DateTime.Parse(Convert.ToString("11/18/2019 10:24AM"); dt = dt.ToLocalTime();
  • 我正在尝试使用 c# 在 oracle 时间戳列中添加上述日期,
  • 请帮我把日期格式化成正确的格式,

标签: c# .net asp.net-mvc c#-4.0 oracle-sqldeveloper


【解决方案1】:

您不应该通过连接字符串来构建 SQL 命令;这不仅难度更大,而且对 SQL 注入开放。

看看使用参数化查询,例如:

DateTime dt = DateTime.Parse(Convert.ToString(CurrentItem[SharePointColumnInternal[j]]));
dt = dt.ToLocalTime();

using (var connection = new OracleConnection("YourConnectionString"))
using (var command = new OracleCommand("UPDATE YourTable SET YourDateTimeColumn = :dt Where ...", connection))
{
    command.Parameters.Add("dt", dt);
    command.ExecuteNonQuery();
}

【讨论】:

  • 感谢它在将内联查询更改为参数后为我工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 2016-08-13
  • 2020-01-14
  • 1970-01-01
相关资源
最近更新 更多