VB2005+ACCESS中,如何获得select count(1) from tb的返回值?
我想取得某table的行数,用的方法如下,希望大家能帮我补充/纠正下:
Dim MyConnection As New OleDbConnection(...)
Dim SQLString As String = "SELECT * FROM table "
MyConnection.Open()
MyCommand = New OleDbCommand(SQLString, MyConnection)
Dim CountString As String = "SELECT count(1) FROM table " & " WHERE ... "
MyCommand.CommandText = CountString
MyCommand.Connection = MyConnection
Dim RowCounts As Integer
RowCounts =??? '我用=MyCommand.ExecuteNonQuery()试了下,不行,msdn找不到,就上来问问了,谢过了啊~
[解决办法]
返回一个值,不该用这个方法ExecuteNonQuery,应该用这个方法: ExecuteScalar
[解决办法]
修改你的代码
Dim CountString As String = "SELECT count(1) as 表达式1 FROM table " & " WHERE ... "
……
……
RowCounts =MyCommand.ExecuteScalar '这里应该用.ExecuteScalar
MSND上说:
MyCommand.ExecuteScalar是“执行查询,并返回查询所返回的结果集中第一行的第一列”
MyCommand.ExecuteNonQuery()是“对连接对象执行 SQL 语句” 它没有返回值
我刚刚测试过