我有多语言应用程序,我使用"en-US"one_answers"ar-KW"语言。我也有WinForm,我正在使用日期时间选择器控件。当用户登录时,如果他的默认语言是"en-US",则保存记录,但当他的默认语言是"ar-KW"时,则此日期格式不会保存在数据库中,并产生错误。为什么?
SQL Server中Employee DateOfBirth字段的数据类型为smallDateTime
。我使用下面的代码在VB中保存记录。后端有一个普通的INSERT
存储过程。
If Me.dtDateOfBirth.Checked = False Then
cmd.Parameters.Add("@DateOfBirth", SqlDbType.SmallDateTime).Value = SqlDateTime.Null
Else
If mUserDefaultLanguage = "ar-KW" Then
Me.dtDateOfBirth.Format = DateTimePickerFormat.Custom
Me.dtDateOfBirth.CustomFormat = "dd/MM/yyyy"
Dim oDate As String = Me.dtDateOfBirth.Text
Dim dt As Date
dt = (CType(oDate, Date))
cmd.Parameters.Add("@DateOfBirth", SqlDbType.DateTime).Value = SqlDateTime.Parse(dt)
Else
cmd.Parameters.Add("@DateOfBirth", SqlDbType.DateTime).Value = SqlDateTime.Parse(Me.dtDateOfBirth.Text)
End If
End If
这可能是日期时间转换的问题:
Dim oDate As String = Me.dtDateOfBirth.Text
Dim dt As Date
dt = (CType(oDate, Date))
为什么不利用DateTimePicker
控件的.Value
属性,而不是将其.Text
属性转换为DateTime
类型?