简体   繁体   English

.NET中的Windows服务异常

[英]windows service exception in .NET

I will be tested the application as windows application then it will be stored the datetime in MySQL data base.When I will be start this application using windows service it will be thrown this exception. 我将测试该应用程序是否为Windows应用程序,然后将日期时间存储在MySQL数据库中。当我使用Windows服务启动此应用程序时,将引发此异常。

error [HY000][MySQL][ODBC 3.51 Driver] [MySqlid -6.0.11-alpha-community]incorrect datetime value " 5/6/2011 9:00:00 AM" for column column-name at row1

Windows application take the system format & my system format is yyyy-MM-dd hh:mm:ss in windows service which format is used. Windows应用程序采用系统格式,而我在Windows服务中使用的格式是yyyy-MM-dd hh:mm:ss。

query18 += "select '" + obj8 + "' as DTvalue ,'" + date8 + "' as DTdatelogged1 ,'" + OpcGroup.QualityToString(e8.sts[counter8].Quality) + "' as DTquality ,'" + DateTime.FromFileTime(e8.sts[counter8].TimeStamp) + "' as DTtimestamp ,'" + e8.sts[counter8].HandleClient + "' as DTparamID Union " + Environment.NewLine;

UpdateQuery = Update parameter t Left join + Environment.NewLine;
                    UpdateQuery8 +=  (  + query18 +  ) Temp on" + Environment.NewLine;
                    UpdateQuery8 += t.itemID=Temp.DTparamID+ Environment.NewLine;
                    UpdateQuery8 += set paramvalue=DTvalue, date_logged1=DTdatelogged1,Quality=                         DTquality,date_logged=DTtimestamp   + Environment.NewLine;
                    UpdateQuery8 += where t.groupID=9 and t.itemID=Temp.DTparamID;

my query likethis timestamp value is 129500892576718750 it will be convert DateTime.FromFileTime() function converted value like '2011-05-17 12:30:57' in windows application it will be write into mysql database but in windows service converted value like 2011/05/17 12:30:57 PM it will be not accepted by the MYSQL database same thing i will be used in the windows service 我的查询这样的时间戳值是129500892576718750,它将在Windows应用程序中转换DateTime.FromFileTime()函数转换后的值,例如“ 2011-05-17 12:30:57”,它将被写入mysql数据库,但是在Windows服务中转换后的值,例如2011 / 05/17 12:30:57 PM它不会被MYSQL数据库接受我将在Windows服务中使用的同一件事

now 现在

UpdateQuery8 = "Update parameter " + Environment.NewLine;
                        UpdateQuery8 += "set paramvalue=@paramvalue,date_logged1=@date_logged1,Quality=@Quality,date_logged=@date_logged" + Environment.NewLine;
UpdateQuery8 += "where groupID=9 and itemID=@itemID";
                        cmd8 = new OdbcCommand(UpdateQuery8, con136);
  cmd8.Parameters.Add("@paramvalue",  obj8.ToString());
  cmd8.Parameters.Add("@date_logged1", date8);
 cmd8.Parameters.Add("@Quality", OpcGroup.QualityToString(e8.sts[counter8].Quality));
  cmd8.Parameters.Add("@date_logged", dt);
  cmd8.Parameters.Add("@itemID",e8.sts[counter8].HandleClient);
  cmd8.ExecuteNonQuery();

it will be execute but there no updation in database 它会执行,但数据库中没有更新

Please help me in this regard. 请在这方面帮助我。

Thanks in Advance. 提前致谢。

Always use parametrized queries to pass data to the DB driver. 始终使用参数化查询将数据传递到DB驱动程序。 Then it is up to the driver to format your dates correctly, and you avoid being susceptible to SQL-Injection attacks. 然后,由驱动程序来正确格式化日期格式,并且可以避免受到SQL-Injection攻击的影响。

Create a datetime and format it the way YOU want. 创建日期时间并以所需的方式对其进行格式化。 Not the system defaults, not the casual user defaults, the one you want. 不是系统默认值,不是临时用户默认值,就是您想要的默认值。

DateTime dt = DateTime.Now;
String str = dt.ToString("yyyyMMdd");

This should lead to "20110517" if i'm not mistaking. 如果我没有记错的话,这应该导致“ 20110517”。

Bonus points are given if you use one of the better answers setting the code locale to the one used by the mysql server. 如果您使用更好的答案之一,将代码区域设置设置为mysql服务器使用的语言环境,则会获得加分。 But the one above should give you a way that works. 但是上面的一个应该给你一种可行的方法。

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

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