简体   繁体   English

Linq 与 ASP.NET MVC 3 中的 MySQL 数据库,将 DateTime 存储到变量中

[英]Linq with MySQL database in ASP.NET MVC 3, storing DateTime into variable

I am using MySQL database to work in ASP.NET MVC 3, i've already set up all requirements and connection is working fine.我正在使用 MySQL 数据库在 ASP.NET MVC 3 中工作,我已经设置了所有要求并且连接工作正常。 This code below is working properly and produce right result :下面的代码工作正常并产生正确的结果

        try
        {
            ViewBag.Model = (from n in _db.mainDatas
                             where n.time_stamp == new DateTime(2010, 11, 3, 0, 0, 15) 
                             select n).Take(10).ToList();
        }catch (Exception e) {
            ViewBag.Error = e;
        }

But when i change this code into:但是当我将此代码更改为:

        DateTime test = new DateTime(2010,11,3,0,0,15);
        try
        {
            ViewBag.Model = (from n in _db.mainDatas
                             where n.time_stamp == test  
                             select n).Take(10).ToList();
        }catch (Exception e) {
            ViewBag.Error = e;
        }

this error message is generated:生成此错误消息:

MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution. MySql.Data.MySqlClient.MySqlException:命令执行过程中遇到致命错误。 ---> MySql.Data.MySqlClient.MySqlException: Unable to serialize date/time value ---> MySql.Data.MySqlClient.MySqlException:无法序列化日期/时间值

I am using MySQL Connector/Net 6.3.6.我正在使用 MySQL 连接器/网络 6.3.6。 Any solution to this problem?这个问题有什么解决办法吗?

It seems to be a problem with the Linq to SQL provider for MySql that you have been using.您一直在使用的 MySql 的 Linq 到 SQL 提供程序似乎存在问题。 In first case the date part is "in" the Expression tree that is generate from your linq query where as in the second case the DateTime is declared out side of the Linq query and hence the generated expression tree will be different from the first case.在第一种情况下,日期部分位于从 linq 查询生成的表达式树中,而在第二种情况下,日期时间在 Linq 查询之外声明,因此生成的表达式树将与第一种情况不同。 Now it depends on the parser of expression tree in the Linq to SQL provider how to handle both the cases and it seems in this case the provider is not able to properly handle the second case expression tree.现在它取决于 Linq 到 SQL 提供者中的表达式树解析器如何处理这两种情况,在这种情况下,提供者似乎无法正确处理第二种情况表达式树。

Ok, after doing some searches and googling like crazy, finally I found the solution for my problem.好的,在疯狂地搜索和搜索之后,我终于找到了解决问题的方法。 Well, it's not a solution actually, because it looks like MySQL Connector/Net 6.3.6 is not doing well with my project (maybe because of my server, database, project configurations, etc) so I use Devart dotConnector instead of MySQL Connector/Net 6.3.6 and everything works like magic.好吧,这实际上不是一个解决方案,因为它看起来像 MySQL Connector/Net 6.3.6 在我的项目中表现不佳(可能是因为我的服务器、数据库、项目配置等)所以我使用 Devart dotConnector 而不是 MySQL Connector/ Net 6.3.6,一切都像魔术一样工作。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM