Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从SQL中检索计数_Sql_Vb.net_Oledb - Fatal编程技术网

从SQL中检索计数

从SQL中检索计数,sql,vb.net,oledb,Sql,Vb.net,Oledb,我试图从select SQL语句中获取计数,但运气不好,如何检索该值 下面是我调用函数的地方: Dim sql3 As String=“select COUNT(*)As countvalue from fat\u prods,其中id\u fat=“+valor.ToString+”“ Dim DADOSTRONADOS3作为System.Data.DataTableReader=buscadadosacess(sql3) oConn.Close() 以下是函数: 函数buscadadosa

我试图从select SQL语句中获取计数,但运气不好,如何检索该值

下面是我调用函数的地方:

Dim sql3 As String=“select COUNT(*)As countvalue from fat\u prods,其中id\u fat=“+valor.ToString+”“
Dim DADOSTRONADOS3作为System.Data.DataTableReader=buscadadosacess(sql3)
oConn.Close()
以下是函数:

函数buscadadosacess(sql作为字符串)
oConn.ConnectionString=strConn
oConn.Open()
如果oConn.State=ConnectionState.Open,则
ACommand=新的OleDbCommand(sql、oConn)
'定义um数据适配器
AAdapter=新的OleDbDataAdapter()
AAdapter.SelectCommand=a命令
'定义e预紧um数据表com os护墙板
ATabela=新数据表()
A自适应填充(阿塔贝拉)
“护墙板基地协会”
xy=ATabela.CreateDataReader
“勒达塔贝拉酒店
'linha=ACommand.ExecuteReader
如果结束
“不正确的护墙板不符合标准。”
返回xy
端函数
我的问题是如何检索countvalue? 如果它是数据库中的某种列,我会像
msgbox(dadosretronados(“id\u fat”))

因此,我真正的问题是我需要在DadosRetronados(此处)中放入什么来获得计数的值?

DadosRetronados(“countvalue”)

DadosRetronados(0)

请尝试以下操作:

Private Function RowCount() As Integer
    ' Declare the object to return
    Dim count As Integer = -1

    ' Declare the connection object
    Dim con As OleDbConnection

    ' Wrap code in Try/Catch
    Try
        ' Set the connection object to a new instance
        ' TODO: Change "My Connection String Here" with a valid connection string
        con = New OleDbConnection("My Connection String Here")

        ' Create a new instance of the command object
        Using cmd As OleDbCommand = New OleDbCommand("SELECT Count([id_fat]) FROM [fat_prods] WHERE [id_fat]=@id_fat", con)
            ' Paramterize the query
            cmd.Parameters.AddWithValue("@id_fat", valor)

            ' Open the connection
            con.Open()

            ' Use ExecuteScalar to return a single value
            count = Convert.ToInt32(cmd.ExecuteScalar())

            ' Close the connection
            con.Close()
        End Using
    Catch ex As Exception
        ' Display the error
        Console.WriteLine(ex.Message)
    Finally
        ' Check if the connection object was initialized
        If con IsNot Nothing Then
            If con.State = ConnectionState.Open Then
                ' Close the connection if it was left open(exception thrown)
                con.Close()
            End If

            ' Dispose of the connection object
            con.Dispose()
        End If
    End Try

    ' Return the row count
    Return count
End Function
此函数的作用是返回行计数,如果失败,则返回-1。它通过对返回单个(计数)值的命令调用
ExecuteScalar
来实现


它还可以通过使用using语句或显式调用
Dispose
方法来清除任何遗留对象。

使用参数以避免sql注入和格式错误。如果只需要数据库中的一个值,请使用。要使用您发布的代码,您必须阅读ATabela的第一行第一列。按照您的建议,查看executeScalar并使其正常工作。谢谢,我如何才能将您标记为正确?很好的解释。为什么不使用…End Using将连接包装在
中?您可以删除异常代码,让任何错误冒泡到调用代码,最终到达UI代码。请不要使用
.AddWithValue
。请参阅和另一个问题:这里是另一个@Mary-关于您的第一个问题,我可能会在生产中以稍微不同的方式实现代码。我不确定OP希望所有的东西都能起作用,所以我基本上什么都给了。至于第二个问题,我可以理解为什么你应该养成添加(…).Value(…)
的习惯,但是为了得到一个计数,交易将是不可见的。不起作用:)我使用了ExecuteCalar,它完成了工作,谢谢你的帮助