Sam Wrayn
Sam Wrayn

Reputation: 63

Execute a stored procedure using an SqlDataAdapter in C#

How can i use an SqlDataAdapter to execute an sql stored procedure that contains statements which update one table and insert into another table, but returns no value?

EDIT:There are answers related to the execution of stored procedures with SELECT statements. However, that's not what i'm looking for.

Upvotes: 1

Views: 2930

Answers (2)

JNYRanger
JNYRanger

Reputation: 7097

I'm assuming you're using the SqlDataAdapter to populate a DataTable or DataSet object. If this is the case and you want the changes to the DataTable/DataSet to be persisted to your database and you require using stored procs then you'd need to set the UpdateCommand, InsertCommand, and/or DeleteCommand properties.

Example:

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM WHATEVER", connection);
adapter.UpdateCommand = new SqlCommand()
{
    Connection = connection,
    CommandText = "EXEC myStoredProc",
    CommandType = CommandType.StoredProcedure
};
//Don't forget to set your parameters!

Otherwise if you don't need the adapter just use a SqlCommand and call ExecuteNonQuery()

Upvotes: 1

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89071

If the statement returns no data, you wouldn't use a SqlDataAdapter. Just use a SqlCommand and call ExecuteNonQuery().

Upvotes: 0

Related Questions