保存记录时更改日期格式

时间:2016-01-06 14:42:22

标签: vb.net ms-access

我正在开发使用MS-ACCESS作为后端的Windows应用程序。

我在MS-ACCESS中有一个表,其中包含Datetime Type Column,它保存了今天的日期。

我有以下代码。 (表格上的代码)

                Dim oCoupon2 As New Coupon
                oCoupon2.LoadByPK(oCoupon.ID)
                oCoupon2.RedeemedDate = System.DateTime.Now
                oCoupon2.IsRedeemed = "Yes"
                oCoupon2.RedeemedCustomerID = .ID
                Dim isSuccsess As Integer
                isSuccsess = oCoupon2.Save()

保存实体的代码(业务层中的代码)

  Public Overrides Function Save() As Boolean
            Try

                Dim bSuccess As Boolean = False
                SetColumn(IDColumnName, _ID)
                SetColumn(CouponNoColumnName, _CouponNo)
                SetColumn(ExpDateColumnName, _ExpDate)
                SetColumn(IsRedeemedColumnName, _IsRedeemed)
                SetColumn(RedeemedDateColumnName, _RedeemedDate)
                SetColumn(RedeemedCustomerIDColumnName, _RedeemedCustomerID)
                bSuccess = MyBase.Save()
                ' Ensures that the _DepartmentID is updated with the new Identity value in case of Insert
                If bSuccess Then _ID = MyBase.IdentityColumn
                Return bSuccess

            Catch ex As Exception

            End Try
        End Function


  Protected Sub SetColumn(ByVal ColumnName As String, ByVal Value As Object)
            Try
                If Not _dict.ContainsKey(ColumnName) Then
                    _dict.Add(ColumnName, Value)
                Else
                    If Value Is DBNull.Value Then Value = Nothing
                    _dict(ColumnName) = Value
                End If
            Catch ex As ArgumentException
                ' Exception occurs when adding a key that already exists in dictionary. Try replace mode.
                Try
                    _dict(ColumnName) = Value
                Catch
                    _Error = ex
                    Throw ex
                End Try
            Catch ex As Exception
                _Error = ex
                Throw ex
            End Try
        End Sub

代码(数据层代码)

Public Overridable Function Save() As Boolean
        Dim bSuccess As Boolean = False
        If _IsAddNew Then
            bSuccess = Insert()
            If bSuccess Then _IsAddNew = False
        Else
            bSuccess = Update()
        End If
        Return bSuccess
    End Function

在VS 2012中保存时会生成以下查询

UPDATE Coupon SET CouponNo = '262752325',ExpDate = '2/20/2016 00:00:00',IsRedeemed = 'Yes',RedeemedDate = '1/6/2016 20:28:31',RedeemedCustomerID = Null WHERE ID = 17

它假设保存今天的数据,即1月1日 - 16日,但保存记录时保存为1月6日 - 16日,请在下面查看...

enter image description here

我已经检查了我的机器的时间,它显示了完美的时间。即2016年1月1日 我必须检查哪些设置?

2 个答案:

答案 0 :(得分:0)

您未能指出您正在使用的数据层(ADO,ADO.Net - OleDB),我怀疑您在Access中使用VBA而不是VB.Net(Visual Studio。)

也就是说,访问更新看起来像这样:

UPDATE a SET a.dt = #2016/3/2#;

#分隔日期字符串。 yyyy / m / d格式避免了本地化问题。

答案 1 :(得分:0)

问题是你的SetColumn函数:

SetColumn(RedeemedDateColumnName, _RedeemedDate)

它生成:

RedeemedDate = '1/6/2016 20:28:31'

虽然它 - 对于Access - 应该创建:

RedeemedDate = #1/6/2016 20:28:31#