2017-02-19 68 views
0

This was the error msg im gettingenter image description here我有一个数据库有一行“Total_Time”(时间)。 格式为HH:MM:SS。我需要将代码“Total_time”转换为分钟。如何将总时间转换为分钟

例如,如果Total_time = 01:30:00,答案应该是Total_minutes = 90, ,我想将total_minutes乘以“Other”(int变量)。

下面是我曾尝试:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 


    con = New System.Data.SqlClient.SqlConnection 
    Try 


     con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False" 
     con.Open() 


     Dim cm As SqlClient.SqlCommand 
     cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE [email protected]", con) 
     cm.Parameters.AddWithValue("@id", TextBox5.Text) 
     dr = cm.ExecuteReader() 
     While dr.Read() 
      Dim tt As Double 
      tt = dr("Total_Time").ToString 
      Dim other As Double 
      other = dr("Other").ToString 

      Dim str() As String 
      Dim strmin As Double 
      str = Split(tt.ToString, ":") 
      strmin = (CDbl(str(1)) * 60 + CDbl(str(2)) + CDbl(str(3))/60).ToString 

      Dim total As Decimal 
      total = strmin + other 
      Label7.Text = total.ToString 
     End While 
    Catch ex As Exception 

    End Try 
End Sub 

但是当我点击什么也没有发生label7没有显示任何值提前 感谢。

+0

这是什么问题? – Roope

+0

我已经插入了这段代码的一个按钮,当我点击什么都没有发生label7是不显示任何值 –

+1

你是不是意味着在年底Label7.Text = strmin –

回答

2
Dim Total_minutes As Double = CDate("1:23:45").TimeOfDay.TotalMinutes   ' 83.75 

为了避免类似的错误,我会强烈建议使用Option Strict

Dim Total_Time As DateTime = Convert.ToDateTime(dr!Total_Time) 
Dim Total_minutes# = Total_Time.TimeOfDay.TotalMinutes 

Dim Other# = Val(dr!Other) 
Dim total# = Total_minutes * Other 

Label7.Text = total.ToString 
+0

我很好奇 - 如果你没有阅读这个问题,你为什么回答? – Chris

+0

@Chris,因为我阅读了标题,并且可能对搜索类似内容的其他人有所帮助 – Slai

+0

我不知道代码如何符合我的代码,请你帮忙..... –

0

尝试以下

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 

    con = New System.Data.SqlClient.SqlConnection 
    Try 
     con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False" 
     con.Open() 

     Dim cm As SqlClient.SqlCommand 
     cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE [email protected]", con) 
     cm.Parameters.AddWithValue("@id", TextBox5.Text) 
     dr = cm.ExecuteReader() 
     While dr.Read()  
      Dim other As TimeSpan   
      Dim tt As TimeSpan 

      If TimeSpan.TryParse(dr("Total_Time"), tt) Then 
       If TimeSpan.TryParse(dr("Other"), other) Then 
        tt = tt.Add(other) 
       Else 
        'Do something like show error message for incorrect data for dr("Other") 
       End If 
       Label7.Text = tt.TotalMinutes.ToString 
      Else 
       'Do something like show error message for incorrect data for dr("Total_Time") 
      End If 
     End While 
    Catch ex As Exception 

    End Try 
End Sub 

如果时间超过24:00:00,使用下面的代码

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 

    con = New System.Data.SqlClient.SqlConnection 
    Try 
     con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False" 
     con.Open() 

     Dim cm As SqlClient.SqlCommand 
     cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE [email protected]", con) 
     cm.Parameters.AddWithValue("@id", TextBox5.Text) 
     dr = cm.ExecuteReader() 
     While dr.Read()  
      Try 
       Dim dataTime As String = dr("Total_Time").ToString 
       dataTime = dataTime.Split("."c)(0).ToString 
       Dim tt As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2))) 
       dataTime = dr("Other").ToString 
       dataTime = dataTime.Split("."c)(0).ToString 
       Dim other As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2))) 
       tt = tt.Add(other) 
       dataTime = tt.TotalMinutes.ToString 

      Catch ex As Exception 
       'do something here as string is not a time 
      End Try 
     End While 
    Catch ex As Exception 

    End Try 
End Sub 
+0

label7.text上仍然没有显示任何内容 –

+0

@vigneshs我认为它应该是'如果dr.Read()然后'语句,如果你正在阅读只有一个记录,并确保查询返回任何结果 – Slai

+0

DId你选择一个正确的ID在文本框5 ??? –

0

不要切换到字符串...并使用Timespan

如果您的标签永远不会更改,那么例程必须出错,从catch语句中检查错误。但是从你的形象看来,他看起来没有任何价值。

而且您应该使用ALWAYS在使用数据库时测试dbnull。 DBNull在Math和Boolean比较中有一些奇怪的行为。

与此

If Not Dr.read OrElse IsDBNull(Dr("Total_time")) OrElse IsDBNull(Dr("Other")) Then 
     Label7.text = "ERR" 
    Else 
     Dim ts As TimeSpan = TimeSpan.Parse(dr("Total_time").ToString) 
     Label7.text = (ts.TotalMinutes * Dr("Other")).ToString 
    End If 

PS替换while循环:你的问题由其他乘说,但你的表格/代码表示添加...我的问题去了。

+0

它是投掷错误 –

+0

我已经uplaoaded错误消息 –

+0

我倒了病,我无法工作的总和一天,但我现在没事了,我已经尝试了所有上面的代码,它不工作 –

相关问题