c# - 程序不允许我使用 MySQL 的 .net 连接器更新 SQL 服务器中的日期时间列

标签 c# mysql sql-server datetime

我正在编写一个与硬件设备交互和监视的程序。我希望能够记录当前状态以及检查时间。

我已经测试过,确保程序与硬件设备、程序与SQL Server之间有连接。而且这种连接很好。

我尝试只更新状态,并且可以找到。但是,当我更新状态和 lastupdated 列 时,它会给出一个内部异常:

"Modifying a column with the 'Identity' pattern is not supported. Column: 'lastupdated'. Table: 'logModel.Store.statuslog_current_log'."

另请注意,SQL 表中的lastupdated 列的类型为DateTime。另外,SQL 服务器是 MySQL 服务器,我在 Visual Studio 2015 上使用 C# 并使用 .Net Connector for MySQL。

这是我正在使用的代码,

if (data != null && data[0] >= 0 && data[1] >= 0 && data[2] >=0 && data[3] >= 0)
{
    List<statuslog_current_display> result = db.statuslog_current_display.Where<statuslog_current_display>(m => m.doorid == DoorID).ToList();
    foreach (statuslog_current_display status in result)
    {
        status.statuscode = (int) data[1];
        status.lastupdated = DateTime.Now;
    }
}
try {
    db.SaveChanges();
} 
catch (Exception ex)
{
    ErrorHandler.showErrorMessage(ex.Message);
}

最佳答案

Allow Zero Datetime=True; 添加到连接字符串的末尾。

当 DATETIME 或 DATE 字段包含无效值时,可能会发生这种情况。根据该列的默认值可以被视为无效日期。

关于c# - 程序不允许我使用 MySQL 的 .net 连接器更新 SQL 服务器中的日期时间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725227/

相关文章:

c# - 在 Visual Studio 2017 : Properties not updated correctly 中从 .csproj 文件创建 .nupkg

c# - 以编程方式创建 ListView 项的绑定(bind)

php - 在 SQL 表中存储包含详细信息的项目数组的最佳方法

mysql - “WHERE”子句不适用于这两种情况

MySQL 更新未设置新标志

sql - SP_WHO2有条件吗?

sql-server - Management Studio 中缺少集成服务目录

c# - 调试 TargetInvocationException

c# - 如何在 Orchard CMS 数据库中保存自定义数据

sql-server - 如何在 SQL Server 中取消嵌套此查询?