Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-Out Parameter for SqlCommand

I have the following parameter for SqlCommand. How do I make it to both in and out the paramter value for the Stored Procedure.

 SqlCommand mySqlCommand = new SqlCommand("aspInsertZipCode", mySqlConnection);
 mySqlCommand.CommandType = CommandType.StoredProcedure;
 mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());
like image 934
LCJ Avatar asked Jul 25 '11 12:07

LCJ


People also ask

How many parameters does a Sqlcommand function have?

Microsoft SQL Server has a limit on the number of parameters that a parameterized query can have (2100).

What is in and out parameter in stored procedure?

An input/output parameter is a parameter that functions as an IN or an OUT parameter or both. The value of the IN/OUT parameter is passed into the stored procedure/function and a new value can be assigned to the parameter and passed out of the module. An IN/OUT parameter must be a variable, not a constant.

What is an output parameter?

Output parameters are the parameters that are fetched from the response of a service call. These are formatted according to the attributes you configure for the output before displaying on the device. The service parameters have a scope and data type attached to them.


3 Answers

var pInOut = mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());
pInOut.Direction = ParameterDirection.InputOutput;

And then to read the output value after you've executed the command:

// assumes that the parameter is a string and that it could possibly be null
string value = Convert.IsDBNull(pInOut.Value) ? null : (string)pInOut.Value;
like image 140
LukeH Avatar answered Oct 17 '22 02:10

LukeH


SqlParameter has a Direction enumeration. Set this value.

Then use the SqlCommand.Parameters.Add that takes a SqlParameter.

Parameter direction:

http://msdn.microsoft.com/en-us/library/system.data.parameterdirection.aspx

You then pull the value out after having called ExecuteNonQuery (for example), by getting the Value from the parameter out of the command collection:

myCommand.Parameters["@paramName"].Value

Can't remember, but I think there is a string indexer on that.

Alternatively, there is this one liner:

myCommand.Parameters.AddWithValue("@paramName", value).Direction = ParameterDirection.InputOutput;

like image 23
Adam Houldsworth Avatar answered Oct 17 '22 04:10

Adam Houldsworth


One of the attributes of a SQL Command Parameter is the Direction. You would want to use (going off of memory)

SqlCommand mySqlCommand = new SqlCommand("aspInsertZipCode", mySqlConnection);
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString());
mySqlCommand.Parameters("@DataRows").Direction = ParameterDirection.InputOutput;
like image 3
Derek Kromm Avatar answered Oct 17 '22 02:10

Derek Kromm