我正在编写一个与硬件设备交互和监视的程序。我希望能够记录当前状态以及检查时间。
我已经测试过,确保程序与硬件设备、程序与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/