vendredi 24 avril 2015

How to compute two time values (one as dispatcher time, second as TIME datatype from DB) and store new result in DB

I am trying to create a simple time tracking application in WPF. I have a DispatcherTimer, which checks every few second which window is active and saves name and time to SQLite database. SO DB(SQLite as I want it to be embedded) looks like this:

Column WindowName, type string, example: MainWindow
Column TimeTotal, type TIME, example: 00:00:05

Trouble comes, when I want to add new time to the old time in the database. Lets say, I have another 5 seconds, which I want to add to the first 5 seconds, so at the end, value in the database should be: 00:00:10. I can't make this work. I found a TimeSpan function and I tried to implement it as follows:

public DispatcherTimer TestDT { get; set; }
public TimeSpan Time { get; set; }
public TimeSpan Together { get; set; }

using (var conn = new SQLiteConnection(connString))
        {
            conn.Open();
            var query = "SELECT TimeTotal FROM TestTable where WindowName = 'MainWindow'";
            using (var cmd = new SQLiteCommand(query, conn))
            {
                using (var reader = cmd.ExecuteReader())   
                {
                    if (reader.HasRows && reader.Read())
                    {
                    Time = (TimeSpan)reader["TimeTotal"];                         
                    }

                    Together = Time + TestDT.Interval;
                }
            }
        }

So that I would take actual value from column TimeTotal as data type TIME and cast it to TimeSpan, then take an actual value in program, compute these two together in program, and insert new timespam (property Together) back to the DB. Problem is that when I try to take the value from DB, it says:

Specified cast is not valid

Value, which should be taken from database is in format "00:00:05". Even if I solve this issue, I have red that maximum value of TimeSpan is 23:59:59. So what would happen if I cross this time value to, lets say, 50 hours?

I can't move from this point and I am not even sure, that my approach is good. I actually think, that is is not. Is there a better way to approach my problem and solve it?

Thanks everyone for cooperation

Aucun commentaire:

Enregistrer un commentaire