SQLテーブルからデータを取得して、テーブルの内容を表示したいビューページに表示しようとしています。
現在、以下のコードを使用してデータベース内のアイテムを読み取ることができます
SqlConnection connection = new SqlConnection(VV);
using (connection)
{
//LIMIT 5 DESC from ID which shows last 5 work outs
SqlCommand myCommand = new SqlCommand("SELECT * FROM Strength", connection);
connection.Open();
SqlDataReader read = myCommand.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
Id = read["Id"].ToString();
System.Diagnostics.Debug.WriteLine(Id);
Weight = read["Weight"].ToString();
System.Diagnostics.Debug.WriteLine(Weight);
Rep = read["Rep"].ToString();
System.Diagnostics.Debug.WriteLine(Rep);
}
}
else
{
Console.WriteLine("nothing");
}
read.Close();
}
そして今、これをビューのHTMLテーブルに表示したいと思います。私は次のようないくつかのことを試しました
ViewBag.HtmlStr = "<table class='table table-striped top-buffer'"
+ "style='width:300px'>"
+ "<tr><th>Weight(KG)</th><th>Reps</th></tr>"
+ "<tr><td>" + TableWeight + "</td>"
+ "</tr><tr><td>" + TableRep + "</td></tr></table>";
しかし、それは私に1行しか与えていません。
何かアドバイス?どうもありがとう
using (connection)
{
//LIMIT 5 DESC from ID which shows last 5 work outs
SqlCommand myCommand = new SqlCommand("SELECT * FROM Strength", connection);
connection.Open();
SqlDataReader read = myCommand.ExecuteReader();
string result = "<table class='table table-striped top-buffer'"
+ "style='width:300px'>"
+ "<tr><th>Weight(KG)</th><th>Reps</th></tr>";
if (read.HasRows)
{
while (read.Read())
{
Id = read["Id"].ToString();
System.Diagnostics.Debug.WriteLine(Id);
Weight = read["Weight"].ToString();
System.Diagnostics.Debug.WriteLine(Weight);
Rep = read["Rep"].ToString();
System.Diagnostics.Debug.WriteLine(Rep);
result += "<tr><td>" + Weight + "</td>"
+ "</tr><tr><td>" + Rep + "</td></tr>";
}
}
else
{
Console.WriteLine("nothing");
}
read.Close();
ViewBag.HtmlStr = result + "</table>";
}
データソースに複数の行が含まれていることを考慮する必要があり、各行をテーブル内のTRとして追加する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加