2012-01-31 71 views
2

這裏是行爲的總結多個結果線而不是一個。值然後顯示在它們各自的位置。顯示當使用while循環

的問題是,我只想顯示一組值。使用while顯示與查詢表中的記錄一樣多的值。下面的示例:

應該顯示什麼:

<div> 
    <div>Service One is Stopped</div> 
    <div>Service Two is Running</div> 
    <div>Service Three is Stopped</div> 
</div> 

實際發生的:

<div> 
    <div>Service One is </div> 
    <div>Service Two is Running</div> 
    <div>Service Three is </div> 
    <div>Service One is </div> 
    <div>Service Two is </div> 
    <div>Service Three is Running</div> 
    <div>Service One is Stopped</div> 
    <div>Service Two is </div> 
    <div>Service Three is </div> 
</div> 

下面是有問題的代碼。如果這是一個微不足道的問題,我很抱歉。我正在擺脫2年以上的不寫代碼的生鏽,有時甚至簡單的事情都會讓你:-)。非常感謝您的幫助!

@using System.Configuration; 
@using System.Data.SqlClient; 
@using System.Data; 
@using System.Linq; 

@{ 
    System.Data.SqlClient.SqlConnection cn = null; 
    cn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString()); 
    cn.Open(); 
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT DISTINCT * FROM servicehealth", cn); 
    var myreader = cmd.ExecuteReader(); 
} 
@{ 
int rowCount = 0; 
} 
@while (myreader.Read()) 
{ 
     rowCount++; 
     string service1status = ""; 
     string service2status = ""; 
     string service3status = ""; 
     string servicename = @myreader["servicename"].ToString(); 

     switch (servicename.ToLower()) 
     { 
      case "serviceone": 
       service1status = @myreader["servicestate"].ToString(); 
       break; 
      case "servicetwo": 
       service2status = @myreader["servicestate"].ToString(); 
       break; 
      case "service3": 
       service3status = @myreader["servicestate"].ToString(); 
       break; 
     } 

<div> 
    <div>Service One is @service1status</div> 
    <div>Service Two is @service2status</div> 
    <div>Service Three is @service3status</div> 
</div> 
} 

編輯:下面是表的問題縮寫快照:

ServiceId     | ServiceName | ServiceState | StartTrackTime | LastUpdated 
1111-11111-111111-11111111 | ServiceOne | Stopped  | 1/30/2012 0:00 | 1/30/2012 17:57 
2222-22222-222222-22222222 | ServiceTwo | Running  | 1/30/2012 0:00 | 1/30/2012 17:57 
3333-33333-333333-33333333 | ServiceThree | Running  | 1/30/2012 0:00 | 1/30/2012 17:57 
+0

擁有servicehealth列和數據將有助於診斷此問題。 – 2012-01-31 05:03:00

+0

好的,完成了。希望這可以幫助!! – mynameisneo 2012-01-31 05:27:44

回答

2

外貌就像你可以在每次迭代期間輸出服務一樣,並將其置於包含div標籤之間。每個找到的服務都應該有一行。

<div> 
@while (myreader.Read()) 
{ 
    string servicestatus = servicestatus = @myreader["servicestate"].ToString(); 
    string servicename = @myreader["servicename"].ToString(); 
    <div>@servicename is @servicestatus</div> 

} 
</div> 
+0

+1 - 更清潔的解決辦法只是在...內聯SQL添加一個'爲了by'條款...... :) – lukiffer 2012-01-31 05:23:24

+0

如果我可以投票這件事100次我會的。: - )。非常感謝!!! – mynameisneo 2012-01-31 05:38:07

0

您需要將您的字符串聲明和div標籤while循環之外:

@{ 
int rowCount = 0; 
string service1status = ""; 
string service2status = ""; 
string service3status = ""; 

while (myreader.Read()) 
{ 
    rowCount++; 
    string servicename = @myreader["servicename"].ToString(); 
    switch (servicename.ToLower()) 
    { 
     case "serviceone": 
      service1status = @myreader["servicestate"].ToString(); 
      break; 
     case "servicetwo": 
      service2status = @myreader["servicestate"].ToString(); 
      break; 
     case "service3": 
      service3status = @myreader["servicestate"].ToString(); 
      break; 
    } 
} 
} 
<div> 
    <div>Service One is @service1status</div> 
    <div>Service Two is @service2status</div> 
    <div>Service Three is @service3status</div> 
</div> 
+0

我想也是如此 - 但是,servicestatus變量無法識別,一旦我做這個 – mynameisneo 2012-01-31 05:03:06

+0

呈現CS0103:'@ {:名稱「service1status」不更新把while循環在同一個代碼塊中的當前上下文 – mynameisneo 2012-01-31 05:04:47

+0

存在while()}'而不是'@while()',因爲它是自包含的範圍。 – lukiffer 2012-01-31 05:08:07