Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-use SqlCommand object for a CommandText after executing a query based on stored procedure in C#?

I have a sample code

aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.AddWithValue("@book_id", bookID);
aCommand.Parameters.AddWithValue("@user_id", userID);

and after that I want to execute a simple query using CommandText:

aCommand.CommandText = "SELECT * FROM aTABLE";
aCommand.ExecuteNonQuery();

but the error occurs:

Exception: Could not find stored procedure 'SELECT * FROM aTABLE'

In this case, I have to create a new instance of SqlCommand object ?

It is a way to use same SqlCommand object to avoid create one ?

like image 289
Snake Eyes Avatar asked Nov 29 '22 02:11

Snake Eyes


1 Answers

It should be

aCommand.CommandType = CommandType.Text

actually, the default value of CommandType is CommandType.Text

like image 197
John Woo Avatar answered Apr 26 '23 23:04

John Woo