在插入时我遇到运行时错误(String未被识别为有效的DateTime。)

编程入门 行业动态 更新时间:2024-10-10 15:21:35
本文介绍了在插入时我遇到运行时错误(String未被识别为有效的DateTime。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将Windows表单文本框转换为Datetime,我得到这个(字符串未被识别为有效的DateTime。)错误请事先解决谢谢...快乐编码

解决方案

首先,不要使用文本框进行日期/时间输入,除非您别无选择:它们是PITA,因为用户可以以他想要的任何格式输入他想要的内容,而且可能不是正确。对于WinForms,使用DateTime选择器 - 它对用户更友好,并且它不能生成坏DateTime值。 但如果必须,然后看看 DateTime.TryParse [ ^ ],它允许你检查(并在可能的情况下转换)一个字符串为DateTime值。 一旦你有了DateTime,你可以通过一个直接传递给SQL参数化查询。

尝试使用 DateTime.TryParse 函数。 以下是一些示例 - DateTime.TryParse方法(字符串,日期时间) [ ^ ] TryParse示例 [ ^ ]

How do I Convert Windows forms Textbox to Datetime, I am Getting this(String was not recognized as a valid DateTime.) error please solve thanks in advance...happy coding

解决方案

First off, don't use a textbox for Date / Time entry unless you have no alternative: they are a PITA as the user can enter what he wants, in whatever format he wants, and it may not be correct. For WinForms, use a DateTime picker instead - it's more friendly for the user, and it can't generate a "bad" DateTime value. But if you must, then look at DateTime.TryParse[^] which lets you check (and convert if possible) a string to a DateTime value. Once you have the DateTime, you can pass that directly to SQL via a parametrized query.

Try using DateTime.TryParse function. Here are some examples - DateTime.TryParse Method (String, DateTime)[^] TryParse examples[^]

更多推荐

在插入时我遇到运行时错误(String未被识别为有效的DateTime。)

本文发布于:2023-11-23 13:24:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入时   未被   错误   String   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!