Shaho
Shaho

Reputation: 182

Is there a way to have a stored procedure parameter that is both input and output in C#?

I'm re-writing a program which was developed in Delphi and I want to code it into C#. The old developer has used a stored procedure which I want to use it like the old version. In the SP's parameters, there are some output parameters but one of those in which has been used as an input parameter too. So I've to set a value to it and also get the output value.

I tried to code like this:

int MyValue = 4;
SqlCommand MyCom = new SqlCommand();
MyCom.Connection = (SomeConnectionObject);
MyCom.CommandType = CommandType.StoredProcedure;
MyCom.CommandText = "MySP";

MyCom.Parameters.AddWithValue("@Parameter1_Output", MyValue);
MyCom.Parameters["@Parameter1_Output"].Direction = ParameterDirection.Output;

In the executed query (Profiler) value set to the parameter is 0 while I have set it to 4.

Upvotes: 1

Views: 352

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

Well, if we have a look at ParameterDirection enum we'll find

Fields

...

InputOutput 3 The parameter is capable of both input and output.

(italic is mine). That's why

MyCom.Parameters["@Parameter1_Output"].Direction = ParameterDirection.InputOutput;

Upvotes: 2

Related Questions