首页 > 解决方案 > 我如何对 SQL Server 表单元格结果 vb.net 上的金额求和

问题描述

嗨助手亲爱的我想你不明白。我在 sql 表中有两列(id,金额)我的金额列中有一些数字,当我运行程序时,我的金额应该显示在 label1 中,当我点击开始计时器按钮时,50 将像秒表一样在 label1 处求和但金额应该是这样的格式 0.0050

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label6.Text += 1
            Dim n1 As Double
            Double.TryParse(Label6.Text, n1)
            Dim result As Double = n1
            Label1.Text = (result / 10000000).ToString(TextBox2.Text)

        End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim cmd As New SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "SELECT * FROM Table1 WHERE id='" & "1" & "'"
            Dim adapter As New SqlDataAdapter(cmd)
            Dim table As New DataTable()
            adapter.Fill(table)
            If table.Rows.Count() > 0 Then

                TextBox2.Text = table.Rows(0)(1).ToString()
                cn.Close()
            End If


        End Sub

标签: sqlsql-servervb.net

解决方案


我不明白你在问什么。文本和代码中的内容太多,根本没有意义。但我确实有一些你要改变的东西开始。

'Start by adding this to your form.
'Much better to keep this in an actual numeric type than a string.
Private timerTicks As Double = 0.0

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    timerTicks += 1.0
    'No idea what you were trying to do with TextBox2. It made no sense in that location.
    'If you used better names for the controls, we might be able to help more
    Label1.Text = (timerTicks / 10000000.0).ToString()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Don't try to re-use the same connection object throughout your form!
    Using cn As New SqlConnection("connection string here")
          ' You're only using one field here: don't transfer to whole row over the network!
          cmd As New SqlCommand("SELECT FieldName FROM Table1 WHERE id= @ID")

        'Match the exact type and length in the database here.
        ' Also, did you want to maybe use the timerTicks value?
        cmd.Parameters.Add("@ID", SqlDbtype.Integer).Value = 1

        cn.Open()
        Dim result = cmd.ExecuteScalar()
        If result IsNot Nothing AndAlso Not IsDBNull(result) Then
            TextBox2.Text = result.ToString()
        End If
    End Using
End Sub

推荐阅读