[英]Convert date/time string value to .NET DateTime
i've this string example value: Sun, 09 May 2010 11:16:35 +0200 我有这个字符串示例值:Sun,2010年5月9日11:16:35 +0200
I've to insert it into MySql Date/Time field. 我要将它插入到MySql日期/时间字段中。
How can i convert it into .NET format (or Mysql format), so i can make my INSERT INTO mydate='2010-05-09 11:16:35'
? 我如何将其转换为.NET格式(或Mysql格式),所以我可以让我的INSERT INTO
mydate='2010-05-09 11:16:35'
? Thank you ! 谢谢 !
The MSDN documentation on the DateTime.Parse()
method describes in detail how to do this. DateTime.Parse()
方法上的MSDN文档详细描述了如何执行此操作。
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
First you need to use DateTime.Parse()
to create a .NET DateTime object from the string value, as noted by others. 首先,您需要使用
DateTime.Parse()
从字符串值创建.NET DateTime对象,如其他人所述。
Don't be tempted to do something like: 不要试图做类似的事情:
var sql = "INSERT INTO MyTable VALUES(" + someDate.ToString() + ")";
It's much better to build a parameterized query instead, not just in this case. 相反,构建参数化查询要好得多,不仅仅是在这种情况下。 It also makes sure that if you're trying to insert/update text, you're able to handle quotes correctly (instead of risking a sql injection possibility)
它还确保如果您尝试插入/更新文本,您可以正确处理引号(而不是冒着SQL注入的可能性)
using (var conn = new MySqlConnection(connectString))
using (var cmd = new MySqlCommand("INSERT INTO mytable VALUES (1, 2, @theDate)", conn))
{
cmd.Parameters.AddWithValue("@theDate", someDate);
cmd.ExecuteNonQuery();
}
System.DateTime dateTime = System.DateTime.Parse(YourDate)
然后你就可以做任何你想要的事情,比如在几秒钟内完成它,或者其他什
事实上, DateTime.Parse()是我脑海中最容易实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.