Execute a stored procedure with no data return
Instead of calling SqlCommand.ExecuteReader()
, call SqlCommand.ExecuteNonQuery()
.
As per this MSDN reference:
ExecuteReader
Executes commands that return rows. For increased performance,
ExecuteReader invokes commands using the Transact-SQL sp_executesql
system stored procedure. Therefore, ExecuteReader might not have the
effect that you want if used to execute commands such as Transact-SQL
SET statements.
ExecuteNonQuery
Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET
statements.
EDIT:
Execute a stored procedure to retrieve data
Or if what you're trying to do is return a scalar value, you can use SqlCommand.ExecuteScalar()
. But if you're looking to get data, you need to utilize a SqlDataReader
object, like so:
Dim queryString As String = "usp_getdata"
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
command.CommandType = CommandType.StoredProcedure
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
' Call Read before accessing data.
While reader.Read()
Console.WriteLine(String.Format("{0}, {1}", _
reader(0), reader(1)))
End While
' Call Close when done reading.
reader.Close()
End Using
SqlDataReader Class Reference