I am not really sure if this is possible or not.
I am currently working on a college project and I have a function that uses stored procedures. I would like to know if it is possible to take the same SqlCommand instance and apply updated parameters to call into the stored procedure again within the same function.
Lets say i have something like this in my code:
myConStr = ConfigurationManager.ConnectionStrings['MyConnString'].ConnectionString; myConn = new SqlConnection(myConStr); myCommand = new System.Data.SqlClient.SqlCommand('team5UserCurrentBooks3', myConn); myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.AddWithValue('@book_id', bookID); myCommand.Parameters.AddWithValue('@user_id', userID); try { myConn.Open(); myCommand.ExecuteNonQuery();
Is it possible to update MyCommand‘s parameters and call the stored procedure again?
Yes. You’ll want to make sure that you call myCommand.Parameters.Clear between each call in order to dump the parameters, but there’s nothing stopping you from reusing the object. (I don’t use C# often, so this may have an error or two in the text)