在插入时我遇到运行时错误(String未被识别为有效的DateTime。) [英] At the time Insertion I am getting run time error (String was not recognized as a valid DateTime.)
本文介绍了在插入时我遇到运行时错误(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 usingDateTime.TryParse
function.
Here are some examples -
DateTime.TryParse Method (String, DateTime)[^]
TryParse examples[^]
这篇关于在插入时我遇到运行时错误(String未被识别为有效的DateTime。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文