繁体   English   中英

使用C#将具有不同ID的表中的数据分配给标签

[英]assign data from table with different id to labels using c#

大家好,我想将表格中的数据分配给一些标签,例如:如果我有3条记录,ID = 1,ID = 2和ID = 3,我希望将此记录分配给3个不同的标签。 ID = 1的数据,label2 = ID = 2的数据等等,如何实现?

未经测试,可能有语法问题,但是您明白了。

string qry="select Column1,Column2,Column3 from YourSchema.YourTable";
using (SQLConnection myConnection=new SQLConnection(Web.ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
{
    using(SQLCommand myCommand=new SQLCommand(qry,myConnection))
    {
        try 
        {
             myConnection.Open(); 
             using(SQLDataReader myReader= myCommand.ExecuteReader())
             {
                 if(myReader.HasRows)
                 {
                      while(myReader.Read())
                      {
                         Label1.Text= myReader[0].ToString();
                         Label2.Text= myReader[1].ToString();
                         Label3.Text= myReader[2].ToString();
                      }
                 }
             }
        }
        catch(SQLException ex)
        {
             // Log exception
             throw ex;
        }  
        finally
        {
            if(myConnection!=null)
                myConnection.Close();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM