简体   繁体   中英

VB.net MYSQL - Count Seconds in Database between 2 times

Hi im having a little problem with vb.net not sure which direction to head really.

The front end of the program is used to count seconds of a certain downtime of a process.

Database has the following table, and an example data:

  • status | startdate | stopdate | Seconds


  • Running 27/08/2012 11:10:11 27/08/2012 11:10:22 11

  • Running 27/08/2012 11:10:11 27/08/2012 11:10:22 11
  • Running 27/08/2012 12:14:27 27/08/2012 12:14:57 30

On the back end all im trying to do is populate a label with the sum of seconds using a datepicker for the date, and when doing the query i use the datepicker text and add the time on to the end of it, to no suprise it didnt work, also it only takes the start date into account, but not the stopdate as im trying to add up within that time frame, and im hopeing someone could help, this is the code i tried below:

Dim DTimePicker1 = DateTimePicker1.text & " 11:00:00"
        Dim DTimePicker2 = DateTimePicker1.text & " 12:00:00"

        cmd.Parameters.Clear()
        cmd.CommandText = "SELECT SUM(seconds) FROM sqlccmlinestatus WHERE status = 'Running' and startdate BETWEEN @BEG AND @END"
        cmd.Parameters.AddWithValue("@BEG", DTimePicker1)
        cmd.Parameters.AddWithValue("@END", DTimePicker1)
        Label70.Text = cmd.ExecuteScalar()

Any help would be great.

Many Thanks, Pete

Dim D1 as Date = DateTimePicker1.Value
Dim D2 as Date = DateTimePicker2.Value
Dim DTimePicker1 as new Date(D1.Year, D1.Month, D1.Day, 11, 0, 0)
Dim DTimePicker2 as new Date(D2.Year, D2.Month, D2.Day, 12, 0, 0)
cmd.Parameters.Clear()
cmd.CommandText = "SELECT SUM(seconds) FROM sqlccmlinestatus WHERE status = 'Running' and startdate BETWEEN @BEG AND @END"
cmd.Parameters.AddWithValue("@BEG", DTimePicker1.ToString("yyyy-MM-dd HH:mm:ss.fff"))
cmd.Parameters.AddWithValue("@END", DTimePicker2.ToString("yyyy-MM-dd HH:mm:ss.fff"))
Label70.Text = cmd.ExecuteScalar()

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