[英]Reading from SQL Server database then reading the top 3 values and output them into labels in asp.net
I am trying to read the top three values from my database and output them into separate labels. 我正在尝试从数据库中读取前三个值,并将它们输出到单独的标签中。
This is the code I have so far: 这是我到目前为止的代码:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["FastHostSS_homeConnectionString"].ConnectionString);
conn.Open();
var readDomain = ("SELECT tldName FROM tld");
SqlCommand com = new SqlCommand(readDomain, conn);
SqlDataReader myReader = com.ExecuteReader();
while (myReader.Read())
{
Domain1_lb.Text = (myReader["TOP 1 tldName"].ToString());
Domain2_lb.Text = (myReader["TOP 2 tldName"].ToString());
Domain3_lb.Text = (myReader["TOP 3 tldName"].ToString());
}
You need to decide which label to assign to based on the loop count. 您需要根据循环计数来确定要分配给哪个标签。
int index = 0;
while (myReader.Read())
{
var tld = myReader["tldName"].ToString();
++index;
if(index == 1) Domain1_lb.Text = tld;
if(index == 2) Domain2_lb.Text = tld;
if(index == 3) Domain3_lb.Text = tld;
}
This is nowadays normally done using data binding, where a framework takes care of the assignments. 如今,通常使用数据绑定来完成此任务,其中框架负责分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.