用毫秒将日期时间插入 SQLCE C#

发布于 2024-09-18 16:21:24 字数 477 浏览 1 评论 0原文

我有一个格式为“dd-MM-yyyy HH:mm:ss.fff”的字符串日期时间,

如下所示..

DateTime mydate = Convert.ToDateTime("13-09-2010 02:30:14.905"); 
 result.SetValue(1, mydate);

我会收到一条错误消息“字符串未被识别为有效的日期时间”

//如果我这样做

DateTime mydate = DateTime.ParseExact("13-09-2010 02:30:14.905", "dd-MM-yyyy HH:mm:ss.fff", CultureInfo.CurrentCulture);
 result.SetValue(1, mydate);

, 工作正常,但格式化它没有毫秒。

我只需要日期时间对象来保存格式化时间,以便我可以插入它。

I have a string datetime in this format "dd-MM-yyyy HH:mm:ss.fff"

like so..

DateTime mydate = Convert.ToDateTime("13-09-2010 02:30:14.905"); 
 result.SetValue(1, mydate);

//I get a error saying 'String was not recognized as a valid DateTime'

if i do it this way

DateTime mydate = DateTime.ParseExact("13-09-2010 02:30:14.905", "dd-MM-yyyy HH:mm:ss.fff", CultureInfo.CurrentCulture);
 result.SetValue(1, mydate);

it works fine but formats it without the milliseconds.

i just need the datetime object to hold my formatted time so i can insert it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2024-09-25 16:21:24

DateTime 不保存“格式化时间” - 它只保存一个时间。就像 int 不保存“十六进制值”或“十进制值”一样 - 它只保存一个整数。

您的第二个版本几乎肯定正确地解析了所有数据 - 将其打印出来进行检查 - 所以我怀疑是数据库交互导致了问题。当然,您可以通过为测试程序显式构造 DateTime 值来将解析与数据库交互隔离开来。

您应该知道,根据 SQL Server CE从 3.5 文档开始,SQL Server CE 中的日期时间类型仅支持百分之三秒的粒度,即刚刚超过 3 毫秒。如果您需要精确毫秒值,您可能需要考虑替代表示(例如精确到秒的日期时间,以及单独的毫秒整数字段。)

DateTime doesn't hold a "formatted time" - it just holds a time. It's like int doesn't hold a "hex value" or a "decimal value" - it just holds an integer.

Your second version is almost certainly parsing all the data correctly - print it out to check - so it's the database interaction that's causing the problems, I suspect. You can isolate the parsing from the database interaction by explicitly constructing a DateTime value for a test program, of course.

You should be aware that according to the SQL Server CE 3.5 docs, the datetime type in SQL Server CE only supports a granularity of one three-hundredth of a second, i.e. just over 3ms. If you need an exact millisecond value, you may want to consider an alternative representation (e.g. a datetime accurate to the second, and a separate integer field for milliseconds.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文