vb.net 使用 VB 从 Access 数据库中检索一列工资的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16452036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:32:11  来源:igfitidea点击:

Using VB to retrieve the sum of a column of salaries from an Access database

vb.netvisual-studio-2010datasetoledb

提问by user2155190

I have been sitting here with five other guys trying out how to total values from a column. We have scoured Google, but none of the code we've found works.

我和其他五个人坐在这里,尝试如何从一列中求和。我们已经搜索过谷歌,但我们发现的代码都不起作用。

Here is my form:

这是我的表格:

form

形式

Here is the code I have thus far:

这是我迄今为止的代码:

Public Class frmTotal
    Dim dt As New DataTable()
    Dim rowindex As Integer = 0
    Dim connstr As String = "provider=microsoft.jet.oledb.4.0;" & "data source=valleyfair.mdb"
    Dim sqlstr As String = "select * from employee"

    Private Sub btnTotal_Click(sender As System.Object, e As System.EventArgs) Handles btnTotal.Click

    End Sub

    Private Sub frmTotal_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dt.Clear()
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlstr, connstr)
        dataAdapter.Fill(dt)
        dataAdapter.Dispose()
    End Sub
End Class

回答by Mitch Wheat

You can either sum in the TSQL or in code. Here's the TSQL version:

您可以在 TSQL 或代码中求和。这是 TSQL 版本:

select *, SUM(MyColumnName) AS TotalMyColumnName
from employee

If you only want the sum then only retrieve the sum and not the other columns

如果您只想要总和,则只检索总和而不是其他列

select SUM(MyColumnName) AS TotalMyColumnName
from employee

回答by Simon Jensen

I'm a rookie at SQL but this is one way i imagine it to be done: 1: Declare variables needed. 2: Fetch needed data from database and save it in a database container. The code only gets values in the column salary. (Unsure what you named it but you could just edit it) 3: using a for loop to add all the numbers together.

我是 SQL 的新手,但这是我想象的一种方法:1:声明需要的变量。2:从数据库中获取需要的数据并保存在数据库容器中。该代码只获取列salary 中的值。(不确定你的名字是什么,但你可以编辑它) 3:使用 for 循环将所有数字加在一起。

If you are using oleDB just change whenever it says SQL to OleDB and it should work just the same

如果您使用的是 oleDB,只需在它对 OleDB 说 SQL 时进行更改,它的工作方式应该相同

Dim Result As Integer = 0
Dim salaryDB As New DataTable
Dim sSQL As String = String.Empty
Dim conn As New SqlClient.SqlConnection
Dim cmd As New SqlClient.SqlCommand
Dim da As New SqlClient.SqlDataAdapter

Public Sub GetSum()
    Try
        conn = New SqlClient.SqlConnection(Constr) 
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        sSQL = "SELECT salary FROM Employee"
        cmd.CommandText = sSQL
        da.SelectCommand = cmd
        da.Fill(salaryDB)
        If dtActive.Rows.Count = 0 Then
            MsgBox("No record found!")
        End If
    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
    For Each Row As DataRow In salaryDB.Rows
        Result = Result + row(0) 'row is the current row selected and 0 is the column number
    Next
End Sub