14

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());

4 Answers 4

16
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;
2
  • public SqlParameter Add(string parameterName,SqlDbType sqlDbType,int size). Based on this, how should I define the size for size? In stored procedure, it is VARCHAR(MAX)
    – LCJ
    Jul 25, 2011 at 12:10
  • 2
    @Lijo: I think that the examples on MSDN use -1 for VARCHAR(MAX) etc. So it would be something like var pInOut = mySqlCommand.Parameters.Add("@DataRows", SqlDbType.VarChar, -1); pInOut.Direction = ParameterDirection.InputOutput; pInOut.Value = dataStringToProcess.ToString();
    – LukeH
    Jul 25, 2011 at 12:17
7

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;

4
  • Should I use mySqlCommand.Parameters[0]? Any other way to define the direction while adding it to the parameter collection itself?
    – LCJ
    Jul 25, 2011 at 12:06
  • @Lijo I've added an example of a one-line command that you can use. Also, Add is deprecated in favour of AddWithValue I believe. Jul 25, 2011 at 12:09
  • AddWithValueis often a bad idea to use and I really don't recommend it, especially if you are working with a string. AddWithValue will create a nvarchar parameter, if you are working with a direct query and you compare your nvarchar parameter to a varchar column it will cause the column to be cast from varchar to nvarchar, this will cause any indexes on that column not to function. Jul 16, 2015 at 23:20
  • @ScottChamberlain I suppose that stands to reason with Unicode being the standard in .NET. That said, there is always the option to fall back onto the other mechanism where you get to state the type. Also, if your stored procedure takes a varchar or other string-like non-nvarchar column, AddWithValue copes with that too. That's an interesting link you've provided, but I disagree with the blanket ban idea. Unit testing and database profiling should cover those issues. Jul 17, 2015 at 7:09
3

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;
1
  • 1
    Typo on last line. Parameters is not a method, it's a collection. Therefore that block should be... SqlCommand mySqlCommand = new SqlCommand("aspInsertZipCode", mySqlConnection); mySqlCommand.CommandType = CommandType.StoredProcedure; mySqlCommand.Parameters.Add("@DataRows", dataStringToProcess.ToString()); mySqlCommand.Parameters["@DataRows"].Direction = ParameterDirection.InputOutput;
    – John Suit
    Sep 16, 2014 at 16:34
0
SqlParameter DataRows = new SqlParameter("@DataRows", SqlDbType.Text) 
{ Value = dataStringToProcess.ToString(), Direction = ParameterDirection.InputOutput};
mySqlCommand.Parameters.Add(DataRows);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.