Unable to convert MySQL date/time value to system. datetime
I believe many people will see this problem. In MySQL databases, such dates are ". Set allow zero datetime = true. This type of date returns a mysqldatetime type in connector-net-5.1.4. The value is naturally 0000-0-0, nullables. when nhibde obtains this value, it tries to convert it to public override object get (idatareader RS, int index)
{
Return new nullabledatetime (convert. todatetime (RS [Index]); // Rs [Index] is an object of mysqldatetime
}
The above error will be thrown.
If "allow zero datetime = true" is not set in the connection string, the above error int Index = Rs. getordinal (name) will be thrown when determining whether it is dbnull );
If (Rs. isdbnull (INDEX ))
{
If (isdebugenabled)
{
Log. debug ("returning null as column:" + name );
}
// Todo: Add a method to nullabletype. getnullvalue-if we want
// Use "magic" numbers to indicate null values
Return NULL;
}
Else
{
Object val = NULL;
Try
{
Val = get (RS, index );
}.
}
I thought that I could find a solution by downloading the source code of connector-net. I didn't expect that a project could not be opened, and the compiled dll could not be added to GAC, so I had to go back and change nullables. nhib.pdf. nullabledatetimetypepublic override object get (idatareader RS, int index)
{
// Return New nullabledatetime (convert. todatetime (RS [Index]);
// Kevin modify
Object OBJ = Rs [Index];
String strname = obj. GetType (). tostring ();
If (strname = "mysql. Data. types. mysqldatetime ")
{
String strval = obj. tostring ();
If (strval. startswith ("0000-0-0 "))
Return NULL;
Else
Return new nullabledatetime (convert. todatetime (strval ));
}
Else
{
Return new nullabledatetime (convert. todatetime (RS [Index]);
}
}