Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CommandType.Text vs CommandType.StoredProcedure

Is there any benefit to explicitly using the StoredProcedure CommandType as opposed to just using a Text Command? In other words, is

cmd = new SqlCommand("EXEC StoredProc(@p1, @p2)");
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@p1", 1);
cmd.Parameters.Add("@p2", 2);

any worse than

cmd = new SqlCommand("StoredProc");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p1", 1);
cmd.Parameters.Add("@p2", 2);

EDIT: Fixed bad copy paste job (again). Also, the whole point of the question is for a data access class. I'd much rather be able to pass the stored proc name and parameters in one line as opposed to extra lines for each parameter.

like image 261
Lurker Indeed Avatar asked Mar 13 '09 17:03

Lurker Indeed


People also ask

What is CommandType StoredProcedure?

CommandType = CommandType. StoredProcedure; Note: Executing stored procedures can be helpful in improving the performance of an application in multi-user and Web applications because a stored procedure executes on the server itself. The Northwind database in SQL server contains a few stored procedures.

What is CommandType text?

The Text commandtype is used when the command is raw SQL, like "select field from table where id=1".

What is CommandType text in C#?

The CommandText property sets or returns a string that contains a provider command, like a SQL statement, a table name, a relative URL, or a stored procedure call.

What is default value of the CommandType property of the Command object?

The CommandType property sets or returns a CommandTypeEnum value that defines the type of the Command object. Default is adCmdUnknown. If you do not specify the type, ADO will need to contact the provider to determine the type of the command.


2 Answers

One difference is how message pumping happens.

Where I used to work we had a number of batch processes that ran over night. Many of them simply involved running a stored procedure. We used to schedule these using sql server jobs, but moved away from it to instead call the procedures from a .Net program. This allowed us to keep all our scheduled tasks in one place, even the ones that had nothing to do with Sql Server.

It also allowed us to build better logging functionality into the .Net program that calls the procedures, so that the logging from all of the overnight processes was consistent. The stored procedures would use the sql print and raiserror functions, and the .Net program will receive and log those. What we learned was that CommandType.StoredProcedure would always buffer these messages into batches of about 50. The .Net code wouldn't see any log events until the procedure finished or flushed the buffer, no matter what options you set on the connection or what you did in your sql. CommandType.Text fixed this for us.

As a side issue, I'd use explicit types with your query parameters. Letting .Net try to infer your parameter types can cause issues in some situations.

like image 128
Joel Coehoorn Avatar answered Sep 18 '22 12:09

Joel Coehoorn


It's cleaner.

You're calling a stored procedure, why not just use the CommandType.StoredProcedure?

like image 23
Jesse Weigert Avatar answered Sep 21 '22 12:09

Jesse Weigert