如何在"ar-KW"区域性时将日期从 Windows 窗体保存到 SQL Server



我有多语言应用程序,我使用"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类型?

最新更新