简体   繁体   中英

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.

And when I look at the stored data of course I find for instance "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

    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#

2013-41-10 10:08 is not a correct datetime.

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:

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. You are getting this because of small mm which means minutes. For months you need to change it to MM and for mintues you need to change it to mm after HH . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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