There are 10 rows in primary_student_table
.
When I execute the following code, the result was -1.
Dim count As Int16
con.Open()
query = "SELECT COUNT(roll) AS rollcount FROM primary_student_table WHERE admityear = 2011 AND batch = 1 "
cmd = New SqlCommand(query, con)
count = cmd.ExecuteNonQuery
MsgBox(count)
con.Close()
What's the problem in the above code?
3条答案
按热度按时间iaqfqrcu1#
You should be using
ExecuteScalar()
rather thanExecuteNonQuery()
because you are fetching a value.For proper coding
using
statement for proper object disposaltry-catch
block to properly handle exceptionsExample Code:
czq61nw12#
The solution is to replace
with
Like Robert Beaubien said in his comments
zbwhf8kr3#
the value in count will be the number of rows in a table :) hope this helped