sql server语言 - 将sql数据绑定单元格转换为带有2位小数的字符串



我目前在Microsoft Visual Studio Express 2013与SQL Server后端工作。我使用SQL查询来拉一个数据表,然后选择第一个单元格来拉一个数字。我需要从数据表中取出这个数字,然后改变一个标签,以显示这个数字作为一个2十进制数字或百分比会更好。下面是我的代码:

    Try
        Dim Associates As Integer = NUPAssociates.Value
        Dim Hours As Integer = NUPHours.Value
        Dim WorkingHours As Integer = (Associates * Hours)
        ' MsgBox(WorkingHours)
        'sql connection
        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("SELECT ((sum([Time])/60)/@WorkHours) as EarnedHours FROM table1 " _
                                              & "Left Join table2 on table1.PartNumber + '-' = table2.PART_NUMBER " _
                                              & "WHERE col1  >= @start AND col1 < dateadd(day, 1, @Start) " _
                                              & "AND col2 = 25 AND col3 = 1 AND FloorNumber is not null and col4 > 30", conn1)
                comm1.Parameters.AddWithValue("@Start", DTPStart.Value.ToString("yyyy-MM-dd"))
                comm1.Parameters.AddWithValue("@WorkHours", WorkingHours)
                Dim dt As New DataTable
                Dim sql As New SqlDataAdapter(comm1)
                sql.Fill(dt)
                Dim EFF As String = dt.Rows(0).Item(0).value.ToString("N2")
                labeleff.Text = EFF
            End Using
            conn1.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

试图使用

访问数据表单元格中的值
dt.Rows(0).Item(0).value.ToString("N2")

,但dt.Rows(0).Item(0)(或简单地dt.Rows(0)(0))已经返回值为object。您使用value属性从DataGridViewCell中检索值。

正确的做法是:

CDbl(dt.Rows(0)(0)).ToString("N2")

相关内容

  • 没有找到相关文章

最新更新