2013-07-31 75 views
1

我正在執行這個存儲過程,我想改變它,而不是僅僅返回所有結果,然後處理它們。我想從存儲過程的第三列返回並顯示信息到我的winforms應用程序的label.text中。我怎麼去做這件事。執行SqlCommand語句並在標籤中顯示結果

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。

回答

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

或不過你要顯示它

+0

應該有()在'結束List ...'我也收到一個錯誤,說明不能從'object'轉換爲'string' for'items.Add(reader [2])' – Masriyah

+0

看到我的更改。 – Jonesopolis

+0

完美地工作 - 謝謝! – Masriyah

0

它將會像reader.GetDecimal(columnIndex)

相關問題