首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

从数据库中调取数据,在规定的地方展示

2013-07-04 
从数据库中调取数据,在规定的地方显示string s ConfigurationManager.ConnectionStrings[database].Co

从数据库中调取数据,在规定的地方显示


            string s = ConfigurationManager.ConnectionStrings["database"].ConnectionString;
            SqlConnection conn = new SqlConnection(s);
            conn.Open();
            string select = "select Name,Intro,Host from ForumIntro where id=1";
            SqlCommand cmd = new SqlCommand(select, conn);
            SqlDataReader myReader = cmd.ExecuteReader();
            while(myReader.Read())
            {
                Label1.Text = myReader["Name"].ToString().Trim();
                Label2.Text = myReader["Intro"].ToString().Trim();
                Label3.Text = myReader["Host"].ToString().Trim();
            }


上面的代码,只能在label1、label2、label3中显示数据库中的第一行数据

怎样设置才能在label4、label5、label6中显示数据库中第二行的数据,依次类推

注:Name,Intro,Host为数据库的列名
[解决办法]
数据库你指定的数据
[解决办法]
引用:

            string s = ConfigurationManager.ConnectionStrings["database"].ConnectionString;
            SqlConnection conn = new SqlConnection(s);
            conn.Open();
            string select = "select Name,Intro,Host from ForumIntro where id=1";
            SqlCommand cmd = new SqlCommand(select, conn);
            SqlDataReader myReader = cmd.ExecuteReader();
            while(myReader.Read())
            {
                Label1.Text = myReader["Name"].ToString().Trim();
                Label2.Text = myReader["Intro"].ToString().Trim();
                Label3.Text = myReader["Host"].ToString().Trim();


            }



上面的代码,只能在label1、label2、label3中显示数据库中的第一行数据

怎样设置才能在label4、label5、label6中显示数据库中第二行的数据,依次类推

注:Name,Intro,Host为数据库的列名

既然有多行数据,用数据控件绑定数据
比如Gridview、repeater来绑定

热点排行