.net 无法将MySQL日期/时间值转换为System.DateTime
我得到这个错误: 无法将MySQL日期/时间值转换为System.DateTime 当我试图从MySQL数据库获取数据时。 我的MySQL数据库中有日期数据类型。但在将其检索到我的datatable中时,它得到了上面的错误.net 无法将MySQL日期/时间值转换为System.DateTime,.net,asp.net,mysql,asp.net-3.5,.net,Asp.net,Mysql,Asp.net 3.5,我得到这个错误: 无法将MySQL日期/时间值转换为System.DateTime 当我试图从MySQL数据库获取数据时。 我的MySQL数据库中有日期数据类型。但在将其检索到我的datatable中时,它得到了上面的错误 如何修复此问题?让MySql将unix时间戳转换为字符串。使用_UNIXTIME(113283901)中的mysql函数将日期时间值作为字符串向下拉,并执行datetime.ParseExact(值,“ddd MMM dd hh:mm:ss yyyy”,区域性,样式)您只需要
如何修复此问题?让MySql将unix时间戳转换为字符串。使用_UNIXTIME(113283901)中的mysql函数将日期时间值作为字符串向下拉,并执行
datetime.ParseExact(值,“ddd MMM dd hh:mm:ss yyyy”,区域性,样式)
您只需要为从数据库返回的日期设置日期格式。很可能是yyyy-MM-dd HH:MM:ss
。至少对我来说是这样
如果我在谷歌上搜索“无法将MySQL日期/时间值转换为System.DateTime”,我会看到许多关于从VisualStudio访问MySQL时出现问题的参考资料。这是你的背景吗
建议的一种解决办法是:
这不是一个bug,而是预期的
行为。请检查下面的手册
连接选项并设置“允许零”
Datetime”设置为true,如所附
图片,错误就会消失
参考:您必须将
Convert Zero Datetime=True
添加到连接字符串中,例如:
server=localhost;User Id=root;password=mautauaja;Persist Security Info=True;database=test;Convert Zero Datetime=True
我添加了两个
Convert Zero Datetime=True
&Allow Zero Datetime=True
并且它工作正常我也遇到了同样的问题,并获得了列名称及其类型。然后从表名中强制转换(col_Name为Char)。通过这种方式,我得到的问题是“0000-00-00 00:00:00”,然后我更新为有效的日期和时间,错误在我的案例中消失。您可以使应用程序与MySql使用的日期和时间完全兼容。当应用程序在运行时运行时,请提供以下代码。
首先转到应用程序事件。
在工具列表中
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
My.Application.ChangeCulture("en")
My.Application.ChangeUICulture("en")
My.Application.Culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
My.Application.Culture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd"
My.Application.Culture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
My.Application.Culture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
End Sub
End Class
您可以使用
MySqlDateTime
对象的IsValidDateTime
属性来帮助您确定是否可以将对象强制转换为DateTime
,而不是更改连接字符串
我有一个场景,我试图从“UpdateTime”列加载数据,该列只有在行发生更新时才显式设置(与总是设置的InsertedTime相反)。对于本例,我使用了MySqlDataReader.GetMySqlDateTime
方法,如下所示:
using (MySqlDataReader reader = await MySqlHelper.ExecuteReaderAsync(...))
{
if (await reader.ReadAsync())
{
DateTime? updateTime = reader.GetMySqlDateTime("UpdateTime").IsValidDateTime ? (DateTime?)reader["UpdateTime"] : null;
}
}
如果“allow zero datetime=true”不起作用,则使用以下解决方案:-
将此添加到您的连接字符串中:“allow zero datetime=no”-这使类型转换非常有效 在Stimulsoft报告中,将此参数添加到连接字符串中 (右键单击数据源->编辑)
在MySql中使用EF edmx时,请查看entitytype中的Precision属性
<EntityType Name="table">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="date_field" Type="datetime" Precision="0" Nullable="false" />
</EntityType>
如果在datetime字段中进行了更改,则可能在属性中缺少此属性。谢谢!仅供参考:这是MySQL连接字符串,而不是SQL Server连接字符串。节省了我的时间。像一个魅力+1-完美的工作!我的datetime为零,因为当列未填充时,在表MySQL上写入datetime为零。我更喜欢从现在起将其更改为0970-01-01。非常感谢您,它如何不起作用?我建议不要使用“0000-00-00 00:00:00”作为日期/时间列中的数据。如果您使用的是.net,请在数据中使用真实的日期值。然而,MySQL可以处理任何日期。您应该扫描所有的日期/时间值。可能的
<EntityType Name="table">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="date_field" Type="datetime" Precision="0" Nullable="false" />
</EntityType>