执行SqlCommand语句并在标签中显示结果



我正在执行这个存储过程,我想更改它,而不是只返回所有结果然后处理它们。我想将存储过程第三列中的信息返回并显示到我的winforms应用程序上的label.text中。我该怎么做呢。

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

我想在存储过程的第3列中显示将返回的项,如下所示: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;
}

应该为你做这件事。然后无论标签在哪里,都有类似的东西

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

或者您希望以何种方式显示

它将有点像阅读器。GetDecimal(columnIndex)

最新更新