http://weblogs.asp.net/andrewrea/archive/2008/02/19/examples-of-using-system-data-parameterdirection-with-sql-server.aspx
Rules to follow
Rule 1
If our ParameterDirection is
ParameterDirection.ReturnValue and we are using in the parameter of the
Procedure or Function it expects the parameter. And we throw this error.
Procedure 'GetTestTable'
expects parameter '@Columnnumeric', which was not supplied.
We should not use the parameter in the procedure if we set
ParameterDirection as ParameterDirection.ReturnValue
in ADO.net.
E.g.
CREATE PROCEDURE [dbo].[GetTestTable] (@Columndatetime DATETIME,
@Columnnumeric NUMERIC)
AS
BEGIN
RETURN 15
END
objCmd.Parameters.Add("@Columndatetime",
SqlDbType.DateTime).Value = objPropertiesClassName.Columndatetime;
objCmd.Parameters.Add("@Columnnumeric",
SqlDbType.Decimal).Value = objPropertiesClassName.Columnnumeric;
objCmd.Parameters["@Columnnumeric"].Direction =
ParameterDirection.ReturnValue;
objCmd.ExecuteNonQuery();
int i = (int)objCmd.Parameters["@Columnnumeric"].Value;
Rule 2
Consider that we have given the parameter order as given below.
The parameter @ColumnnTest will contain the return value. We will think that we have specified
the direction ReturnValue to @Columnnumeric before @ColumnnTest and @Columnnumeric will contain the value. But it is
not like that. Since we added the parameter @ColumnnTest before @Columnnumeric.
It took according to the order of parameter that we are adding to the command.
objCmd.Parameters.Add("@ColumnnTest",
SqlDbType.Decimal).Value = objPropertiesClassName.Columnnumeric;
objCmd.Parameters.Add("@Columnnumeric",
SqlDbType.Decimal).Value = objPropertiesClassName.Columnnumeric;
objCmd.Parameters["@Columnnumeric"].Direction =
ParameterDirection.ReturnValue;
objCmd.Parameters["@ColumnnTest"].Direction =
ParameterDirection.ReturnValue;
int j = (int)objCmd.Parameters["@ColumnnTest"].Value;
object i = (object)objCmd.Parameters["@Columnnumeric"].Value;
Rule 3
objCmd.Parameters.Add("@ColumnnTest",
SqlDbType.Decimal).Value = 20.0d;
objCmd.Parameters.Add("@Columnnumeric",
SqlDbType.Decimal).Value = 10.0d;
objCmd.Parameters["@Columnnumeric"].Direction =
ParameterDirection.ReturnValue;
objCmd.Parameters["@ColumnnTest"].Direction =
ParameterDirection.ReturnValue;
If the procedure or function
doesn’t return any value, the first added parameter tooks the default value.
But the next parameter took
the value that we asigned.
Here
@ColumnnTest returns 0.0d, while ("@Columnnumeric
returns the assigned value 10.0d.
We can ask instead of ReturnValue can we use ExecuteScalar(), since it also returns
single value.
The ExecuteScalar returns a single value that is in the form of a result set. That
means the value must be "SELECTed" in the SQL.
I.e. if the result-set contains 3 columns and 2 rows, it took only the 1st
row and 0th column value. The remaining are discarded.
Using the RETURN keyword is possible through the use of a ReturnValue parameter.
No comments:
Post a Comment