C#.NET 查询MYSQL报错:值类型与列类型不匹配不能在列中存储<0001-01-01 00:00:00>。所需类型是MySqlDateTime


所需类型是MySqlDateTime



扫一扫加微信


解决方案:
1. 检查表结构的日期时间类型,默认值。
[不是null] 若是打勾的,必须把默认设置为“CURRENT_TIMESTAMP”。
[不是null] 不打勾的,必须把默认设置为空值,或者是NULL。
!!!! 绝对不允许'0000-00-00 00:00:00'值,否则.NET无法解析。

另外注意:
1. 若是datetime类型的字段,不要勾选【根据时间戳更新】,否则,您修改任一字段的值,此字段的值会同步更新为当前时间。
2. TimeStamp类型的字段,必须勾选【根据时间戳更新】,默认值为:CURRENT_TIMESTAMP。
可以用下面的语句查询默认值为'0000-00-00 00:00:00'的表:
SQL Code:
SELECT * FROM information_schema.`COLUMNS` where 1=1
and TABLE_SCHEMA in ('cs5_normal','cs5_system')
and column_default='0000-00-00 00:00:00'
and column_type in ('datetime')
order by table_name
//来源:C/S框架网 | www.csframework.com | QQ:23404761
and TABLE_SCHEMA in ('cs5_normal','cs5_system')
and column_default='0000-00-00 00:00:00'
and column_type in ('datetime')
order by table_name
//来源:C/S框架网 | www.csframework.com | QQ:23404761
2. MySQL的连接字符串加上以下属性:
Convert Zero Datetime=True;
删除此属性!!! --> Allow Zero Datetime=True;
C# Code:
//解决方案:
//连接字符串删除:Allow Zero Datetime=True; 加上:Convert Zero Datetime=True;
string conn = "Server={0};Port={1};Database={2};User ID={3};Password={4};Connection TimeOut={5};Charset=utf8;Pooling=true;Min Pool Size=0;Max Pool Size=500;Connection Lifetime=0;Convert Zero Datetime=True;";
conn = string.Format(conn, "120.79.85.97", "3306", "cs5_system", "root", "lucky2021.", 30);
IDatabase db = DatabaseFactory.CreateMySqlDatabase(conn);
var DT = db.GetTable("SELECT * FROM tb_MyUserRoles LIMIT 10;");
var row = DT.Rows.Add();
row["CreateTime"] = DateTime.Now;
DT.AcceptChanges();
MessageBox.Show(row["CreateTime"].ToString());
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//连接字符串删除:Allow Zero Datetime=True; 加上:Convert Zero Datetime=True;
string conn = "Server={0};Port={1};Database={2};User ID={3};Password={4};Connection TimeOut={5};Charset=utf8;Pooling=true;Min Pool Size=0;Max Pool Size=500;Connection Lifetime=0;Convert Zero Datetime=True;";
conn = string.Format(conn, "120.79.85.97", "3306", "cs5_system", "root", "lucky2021.", 30);
IDatabase db = DatabaseFactory.CreateMySqlDatabase(conn);
var DT = db.GetTable("SELECT * FROM tb_MyUserRoles LIMIT 10;");
var row = DT.Rows.Add();
row["CreateTime"] = DateTime.Now;
DT.AcceptChanges();
MessageBox.Show(row["CreateTime"].ToString());
//来源:C/S框架网 | www.csframework.com | QQ:23404761

扫一扫加微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网