Вопрос

I am executing this stored procedure and i want to change it so instead of just returning all of the results and then handling them. I want to return and display the information in the third column from the stored procedure into a label.text on my winforms app. How can i go about doing that.

public IEnumerable GetGuids(int id)
{
    using (SqlCommand _command = new SqlCommand("StoredProc"))
    {
        _command.Connection = new SqlConnection(conString);
        _command.Connection.Open();
        _command.CommandType = CommandType.StoredProcedure;
        _command.Parameters.AddWithValue("@ItemID", id);

        return _command.ExecuteReader(); 
    }
}

I want to display the items that will be returned in the 3rd column from the stored procedure as follows: itemRow1/itemRow2.

Это было полезно?

Решение

public IEnumerable GetGuids(int id)
{    
  List<string> items = new List<string>();        
  using (SqlCommand _command = new SqlCommand("StoredProc"))
  {
      _command.Connection = new SqlConnection(conString);
      _command.Connection.Open();
      _command.CommandType = CommandType.StoredProcedure;
      _command.Parameters.AddWithValue("@ItemID", id);

      using (var reader = _command.ExecuteReader())
      {
         while (reader.Read())
         {
            items.Add(reader[2].ToString());
         }
      }        
  }
  return items;
}

should do it for you. Then wherever the label is, something like

label.Text = String.Join(",", items.ToArray());

or however you want to display it

Другие советы

It will be something like reader.GetDecimal(columnIndex)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top