Why should I use SqlCommand.CommandType = StoredProcedure?

Question: What is the difference between using standard SQLCommandand SQLCommand.ComandType = StoredProcedure?

Since I'm not sure that the parameters are passed to the command object by name or in order, I prefer this:

SqlCommand oCmd = new SqlCommand("exec sp_StoredProcedure @Param1, @Param2, @Param3", oDBConnection);
oCmd.Parameters.Add("Param1", SqlDbType.Bit).Value = var_param1;
oCmd.Parameters.Add("Param2", SqlDbType.NVarChar).Value = var_param2;
oCmd.Parameters.Add("Param3", SqlDbType.NVarChar).Value = var_param3;

but not

SqlCommand oCmd = new SqlCommand("sp_StoredProcedure", oDBConnection);
oCmd.CommandType = StoredProcedure;
oCmd.Parameters.Add("Param1", SqlDbType.Bit).Value = var_param1;
oCmd.Parameters.Add("Param2", SqlDbType.NVarChar).Value = var_param2;
oCmd.Parameters.Add("Param3", SqlDbType.NVarChar).Value = var_param3;
//Do the parameter names and the parameter order matter here?

I do not understand why I should do the second?

+4
source share
2 answers

- , ( ) , , . (), . , ( TSQL), , , ; . , oCmd.Parameters, - , , , .

, .


Re pass-by-name pass-by-position, exec TSQL. :

exec MyProc 'abc', 123

exec MyProc @foo='abc', @bar=123

- ; 'abc' MyProc, 123 MyProc. , .

- ; 'abc' MyProc, @foo, 123 MyProc, @bar. , .

, :

exec sp_StoredProcedure @Param1, @Param2, @Param3

:

exec sp_StoredProcedure @Param1=@Param1, @Param2=@Param2, @Param3=@Param3

.

+7

Microsoft:

... , , .

, CommandType = StoredProcedure , , , , , , , , .

0

Source: https://habr.com/ru/post/1533133/


All Articles