Correct DateTime format in SQL Server CE?

风流意气都作罢 提交于 2019-12-04 07:21:02

If you're worried about getting the format right at all, something has already gone seriously wrong. There are two things you need to do to correctly work with datetime values in any database, not just sqlce:

  1. Make sure you're using a datetime type for the column (not a text type like varchar)
  2. Make sure you're using a datetime parameters in a parameterized query, and not string concatenation.

If you do that, there is no formatting involved on your part. At all. Example:

 void SetDate(int recordID, DateTime timeStamp)
 {
    string SQL = "UPDATE [sometable] SET someDateTimeColumn= @NewTime WHERE ID= @ID";

    using (var cn = new SqlCeConnection("connection string here"))
    using (var cmd = new SqlCeCommand(SQL, cn))
    {
        cmd.Parameters.Add("@NewTime", SqlDbType.DateTime).Value = timeStamp;
        cmd.Parameters.Add("@ID", SqlDbType.Integer).Value = recordID;

        cn.Open();
        cmd.ExecuteNonQuery();
    }
} 

Never ever ever ever EVER use string manipulation to substitute values into sql queries. It's a huge no-no.

Try the following format:

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")

Man, you do not need to convert string to DateTime.

Use a instance of a new DateTime and pass the date as parameter. Like this:

using (var ctx = new DBPreparoEntities())
            {
                var _client = from p in ctx.Client
                                 select new Client
                                 {
                                     data = new DateTime(2016,08,17),
                                     dateconf = null,
                                     scod_cli = p.Rua,
                                     valorini = 7214.62m,
                                 };
                return client.ToList();
            }

don't use:

... data = DateTime.Parse("2016/12/10") // or other type convertions.
Phil

sorry this is in vb.net, but this is a method i use to convert from a CE date/time format:

    Public Shared Function ConvertSqlDateTimeFormat(ByVal s As String) As String
    Dim theDate As New Text.StringBuilder
    Dim sTemp As String = ""
    Dim iIndex As Integer

    If s.Length > 8 Then
        'first we do the time
        iIndex = s.IndexOf(" ", System.StringComparison.OrdinalIgnoreCase)
        If iIndex > 0 Then
            sTemp = s.Substring(iIndex).Trim
            iIndex = sTemp.IndexOf(".", System.StringComparison.OrdinalIgnoreCase)
            If iIndex > 0 Then
                sTemp = sTemp.Substring(0, iIndex)
            End If
        End If

        'next the date
        theDate.Append(s.Substring(4, 2))
        theDate.Append("/")
        theDate.Append(s.Substring(6, 2))
        theDate.Append("/")
        theDate.Append(s.Substring(0, 4))
        theDate.Append(" ")
        theDate.Append(sTemp)
    End If
    Return theDate.ToString
End Function
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!