sql - 我如何对 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
解决方案
我不明白你在问什么。文本和代码中的内容太多,根本没有意义。但我确实有一些你要改变的东西开始。
'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
推荐阅读
- javascript - 决定全局变量何时应该在函数内部或函数外部
- kubernetes - 在 GKE 集群上使用 Terraform 部署 Helm 工作负载
- javascript - 如何在 JavaScript 中按顺序编写返回参数的函数?
- reactjs - React 中的默认状态是如何设置的?
- php - 打印访客当前日期
- python - 子串切片
- r - 在 R 中使用 p-1 < df < p 从 Wishart 分布中采样
- c# - C#:如何在单击按钮之前停止我的程序
- python - 查找列表中的第二大元素(对于重复元素)
- android - java.lang.StackOverflowError:设置从相机捕获背景图像时堆栈大小为 8MB