本文介绍了我怎样才能得到总或总结我的数据库访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以过滤数据库并将其显示到datagridview

i have this code which filter and display the database to datagridview

例如.

score | scoretotal
25    | 30
20    | 40
25    | 25

现在,我怎样才能获得测验的总数和scoretotal"的总数,或者我应该说,如何获得只有 datagridview 显示的总数?

now, how can I get the total of Quiz and total of "scoretotal" or should I say, how to get the total on what only the datagridview displayed?

Private Sub datagrid()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim sSQL As String = String.Empty
    Try
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        sSQL = "SELECT  score as score, total as total FROM prelimquiz"
        sSQL = sSQL & " where username like '%" & Administrator.lblusername.Text & "%' and studentid like '%" & Me.Label25.Text & "%'"
        cmd.CommandText = sSQL
        da.SelectCommand = cmd
        da.Fill(dt)
        Me.DataGridView1.DataSource = dt
        If dt.Rows.Count = 0 Then
        End If
    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

任何意见和建议将不胜感激

any advice and suggestions will be greatly appreciated

推荐答案

By linq:

 Dim dtAsEnum = dt.AsEnumerable
 Dim ScoreResult = dtAsEnum.Sum(Function(row) row.Field(Of Integer)("score"))
 Dim TotalResult = dtAsEnum.Sum(Function(row) row.Field(Of Integer)("total"))

这篇关于我怎样才能得到总或总结我的数据库访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:19