【问题标题】:How I sum the amount on SQL Server table cell result vb.net我如何对 SQL Server 表单元格结果 vb.net 上的金额求和
【发布时间】:2020-05-13 19:35:51
【问题描述】:

嗨助手亲爱的我想你不明白。我在 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

【问题讨论】:

  • 尝试重命名控制变量,以便我们了解它们的用途。我们没有足够的工作继续下去,试图从“TextBox1”之类的名称中推断出含义。
  • 使用定时器的目的是什么?

标签: sql sql-server vb.net


【解决方案1】:

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

'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

【讨论】:

  • 查看此处了解为什么不重复使用相同的连接对象:softwareengineering.stackexchange.com/questions/142065/…
  • 亲爱的我想你不明白。我在 sql 表中有两列(id,金额)我的金额列中有一些数字,当我运行程序时,我的金额应该显示在 label1 中,当我点击开始计时器按钮时,50 将像秒表一样在 label1 处求和但金额应该是这样的格式 0.0050
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2013-09-22
  • 2020-11-23
  • 2015-06-23
  • 1970-01-01
  • 2011-04-29
相关资源
最近更新 更多