這裏是行爲的總結多個結果線而不是一個。值然後顯示在它們各自的位置。顯示當使用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
擁有servicehealth列和數據將有助於診斷此問題。 – 2012-01-31 05:03:00
好的,完成了。希望這可以幫助!! – mynameisneo 2012-01-31 05:27:44