Subscribe

RSS Feed (xml)

Executing a Stored Procedure with No Results Returned

' Set up the connection / command


Dim connectionString as String = "server=.;database=Northwind;


Trusted_Connection=true"


Dim connection As New SqlConnection(connectionString)


Dim command As New SqlCommand("IncrementDownloads", connection)


command.CommandType = CommandType.StoredProcedure


' Execute, but don't return any results


connection.Open()


command.ExecuteNonQuery()


connection.Close()


Comments


There are many cases, especially when using stored procedures, where it is unnecessary


to return a result set from the database. In those cases, the


SqlCommand provides a simple


ExecuteNonQuery() method that can be used to run the results on the database.This


method doesn't return a result set-if one is returned it's simply ignored.


This technique is also useful when executing non-SELECT SQL scripts, such as


UPDATE, INSERT, and DELETE, where returning a result set serves no purpose.


When working with a datareader, make sure to close the connection. Unlike a


dataset, the connection stays open until you close it.

No comments:

Archives

Variety in the Web World