简体   繁体   English

这是sqlite date或c#的编码问题吗?

[英]Is this an encoding issue with sqlite date or c#?

I have a job that runs and records a date at the start and end of a process. 我有一份可以在流程开始和结束时运行并记录日期的作业。 However when the application loads it needs to fetch the data for the previous run. 但是,在应用程序加载时,它需要获取上一次运行的数据。 But every-time it throws an exception saying: String was not recognized as a valid DateTime. 但是每次都会抛出一个异常说:字符串未被识别为有效的DateTime。

And when I look at the stored data of course I find for instance "2013-41-10 10:08" 当然,当我查看存储的数据时,我会发现例如“ 2013-41-10 10:08”

previously set thus 预先设定

string jobsDate = DateTime.Now.ToString("yyyy-mm-dd HH:MM");

The statement I am using is: 我正在使用的语句是:

bool IsInserted = db.Update("UPDATE jobs SET JobLastRun ='" + jobsDate + "', jobStatus = '" + status + "' WHERE rowid = " + rowid );

db.Update is largely redundant but is here for posterity db.Update在很大程度上是多余的,但在此不作赘述

    public bool Update(String updateQuery)
    {
        Boolean returnCode = true;
        try
        {
            //dbl.AppendLine(sql);
            dbl.AppendLine(updateQuery);
            this.ExecuteNonQuery(updateQuery);
        }
        catch(Exception crap)
        {
            OutCrap(crap);
            returnCode = false;
        }
        return returnCode;
    }


    public int ExecuteNonQuery(string sql)
    {
        SQLiteConnection cnn = new SQLiteConnection(dbConnection);
        cnn.Open();
        SQLiteCommand mycommand = new SQLiteCommand(cnn);
        mycommand.CommandText = sql;
        int rowsUpdated = mycommand.ExecuteNonQuery();
        cnn.Close();
        return rowsUpdated;
    }

Anyone? 任何人?

Is this an encoding issue? 这是编码问题吗?

No. You have the minutes and months format specifiers the wrong way around. 否。您使用分钟和月份格式说明符的方式是错误的。 It should be 它应该是

  string jobsDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                                                ^^       ^^
 MM = Months
 mm = minutes
 HH = hours in 24 hour clock
 hh = hours in 12 hour clock

A good reference is: String Formatting in C# 一个很好的参考是: C#中的字符串格式

2013-41-10 10:08 is not a correct datetime. 2013-41-10 10:08不是正确的日期时间。

It was stored that way because of your formatstring here: 由于您的格式字符串在此处存储,因此:

string jobsDate = DateTime.Now.ToString("yyyy-mm-dd HH:MM");

In a formatstring 'MM' means Months, and mm means minutes, so the correct string should be: 在格式字符串中,“ MM”表示月份,“ mm表示分钟,因此正确的字符串应为:

string jobsDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

Is this an encoding issue? 这是编码问题吗?

definitely not . 绝对不是 This is your format that is causing you issues. 这是引起您问题的格式。

if you look at your code you will find that 如果看一下代码,您会发现

2013-41-10 10:08

41 definitely cannot be months. 41绝对不能是几个月。 You are getting this because of small mm which means minutes. 您得到这个是因为mm较小,这意味着几分钟。 For months you need to change it to MM and for mintues you need to change it to mm after HH . 几个月后,您需要将其更改为MM ,而薄荷糖则需要在HH之后将其更改为mm you need to change it like this 您需要像这样更改

string jobsDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm");  //note the capital MM for months and small mm for mintues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM