使用vb.net在SQL中保存日期 [英] date saving in SQL using vb.net
本文介绍了使用vb.net在SQL中保存日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码从文本框中保存日期,然后使用日期选择器选择日期。
I m using the following code to save the date from a textbox and selecting the date using date picker.
If (String.IsNullOrEmpty(DobTxt.Text)) Then
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DBNull.Value
Else
Dim DOBDte As date= String.Format("{0:YYYY-MM-dd}", DobTxt.Text.Trim())
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DOBDte
End If
结束,现在代码可以很好地处理日期,例如
Now the code works just fine with dates like ""
,但是当您输入 10/01/2016之类的日期时,会出现此错误:
but when you go for a date like "10/01/2016" I get this error:
从字符串 10/30 / 2016键入日期无效
Conversion from string "10/30/2016" to type 'Date' is not valid
请帮忙
推荐答案
使用 TryParse
将文本值转换为日期。
Use TryParse
to convert the text value to a date.
Dim dateValue As Date
If String.IsNullOrWhiteSpace(DobTxt.Text) Then
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DBNull.Value
ElseIf Date.TryParse(DobTxt.Text.Trim(), dateValue) Then
SQLCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = dateValue
Else
' alert the user that there is invalid input
End If
这篇关于使用vb.net在SQL中保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文