Vb.net 在MS Access数据库中选择最大值

Vb.net 在MS Access数据库中选择最大值,vb.net,ms-access,Vb.net,Ms Access,我需要在我的行列中选择最大值。当我触线时 (FindCurrentTimeCard = Val(myreader("Row")) 我得到一个错误: System.IndexOutOfRangeException 代码: 问题是,当您计算聚合函数或任何对一个或多个字段执行某些操作的表达式时,除非另有说明,否则此类计算的结果将被分配一个别名,如Expr1000 因此,在计算SQL语句时: 从表2中选择maxtable2.row MS Access将返回分配给别名(如Expr1000)的结果: 因

我需要在我的行列中选择最大值。当我触线时

(FindCurrentTimeCard = Val(myreader("Row")) 
我得到一个错误:

System.IndexOutOfRangeException

代码:


问题是,当您计算聚合函数或任何对一个或多个字段执行某些操作的表达式时,除非另有说明,否则此类计算的结果将被分配一个别名,如Expr1000

因此,在计算SQL语句时:

从表2中选择maxtable2.row MS Access将返回分配给别名(如Expr1000)的结果:

因此,SQL语句不会输出名为Row的列,导致代码在尝试检索该列的值时失败:

FindCurrentTimeCard=ValmyreaderRow 相反,您应该指定代码中可能引用的别名,例如:

从表2中选择maxtable2.row作为maxofrow 然后使用函数返回与该列关联的值:

FindCurrentTimeCard=Valmyreadermaxofrow
在线评论和解释

Public Function FindCurrentTimeCard() As Integer
    Dim CurrentTimeCard As Integer
    'The Using block ensures that your database objects are closed and disposed
    'even it there is an error
    'Pass the connection string directly to the connection constructor
    Using myconnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource=S:\Docs\PRODUCTION\Shop Manager\Shop_Manager\Shop_Manager\Database2.accdb;")
        Dim query As String = "Select MAX(Row) from Table2"
        Using cmd As New OleDbCommand(query, myconnection)
            Try
                myconnection.Open()
                'since you are only retrieving a single value
                'you can used .ExecuteScalar which gets the value
                'in the first row, first column
                CurrentTimeCard = CInt(cmd.ExecuteScalar)
            Catch ex As OleDbException
                MessageBox.Show("Error Pull Data from Table2")
            End Try
        End Using
    End Using
    'vb.net uses the Return statement to return the value of the function
    Return CurrentTimeCard
End Function

MAXRow与Row不是一回事。尝试改用ExecuteScalar。旁注:如果代码引发异常,则连接不会关闭。考虑使用“使用结束”使用块来自动关闭和处理这些对象,这些对象在异常情况下也会关闭。我将表添加到PASTABLE表2中,我不确定您的意思是MAXRow与行不是同一回事。在这种情况下,您应该使用ExcuteSalar,正如拉尔斯所说的那样。但如果您要从查询返回多个值,则应该使用类似于select maxrow as maxrow、OTHERCOLUMIN、from。。。因此,您可以通过列名访问聚合列的值您在查询中返回的列不称为行,因此读取器引发该异常。那么该异常称为什么?@marc_s i在e之前,在c之后除外。谢谢你的更正。红脸-
Public Function FindCurrentTimeCard() As Integer
    Dim CurrentTimeCard As Integer
    'The Using block ensures that your database objects are closed and disposed
    'even it there is an error
    'Pass the connection string directly to the connection constructor
    Using myconnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource=S:\Docs\PRODUCTION\Shop Manager\Shop_Manager\Shop_Manager\Database2.accdb;")
        Dim query As String = "Select MAX(Row) from Table2"
        Using cmd As New OleDbCommand(query, myconnection)
            Try
                myconnection.Open()
                'since you are only retrieving a single value
                'you can used .ExecuteScalar which gets the value
                'in the first row, first column
                CurrentTimeCard = CInt(cmd.ExecuteScalar)
            Catch ex As OleDbException
                MessageBox.Show("Error Pull Data from Table2")
            End Try
        End Using
    End Using
    'vb.net uses the Return statement to return the value of the function
    Return CurrentTimeCard
End Function