Stored output parameter proc + Choose do not pass inverse output parameter

I have a stored procedure that returns two sets of records that I call using GetReader. I repeat first, call IDataReader.NextResult (), and then iterate over the second.

I assign values ​​to the output parameters in sp, but when I check the values ​​after finishing my reading, my output parameters are zero. Looks like a mistake. I don’t want to use select because I don’t like fudge. Some fragments ...

...

sp.Command.AddParameter("@SelectedTabID", selectedTabID, DbType.Int32);
sp.Command.AddParameter("@CurrentTabID", 0, DbType.Int32, ParameterDirection.Output);
sp.Command.AddParameter("@TypeID", 0, DbType.Int32, ParameterDirection.Output);

(pay attention to the implementation of this method or using AddOutputParameter () gives the same results)

...

using(IDataReader reader = sp.GetReader())
{
  while (reader.Read()) {...}
  if (reader.NextResult()) {while (reader.Read()) {...}}

}

...

int one = (int)sp.OutputValues[0]; //null expected an int
int two = (int)sp.OutputValues[1]; //null expected an int

Looking forward to some gems of wisdom :)

+3
source share
5

- . , , sproc, :

SqlCommand cmd = new SqlCommand();
cmd.Connection = ConfigurationManager.ConnectionStrings["MyCon"].ToString();
cmd.CommandText = "MySproc";

SqlParameter parmOut = new SqlParameter("@StuffIWant", SqlDbType.Int);
parmOut.Direction=ParameterDirection.Output; 

cmd.Parameters.Add(parmOut);
cmd.ExecuteNonQuery();

string theThingIWant = parmOut.Value.ToString();

datareader, , . .

+4

, SQLConnection/Command

0

... - ? Reader , , , ...

"ExecuteNonQuery()" , CommandType (CommandType.StoredProcedure)

0

, , , .

:

  • , SqlDataReader cmd.ExecuteReader() , ...
  • , , SqlDataReader reader.Close()
0
source

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


All Articles