把数据库中的数据读出来并用BulletedList控件来显示

来源:互联网 发布:杭州知昀服饰有限公司 编辑:程序博客网 时间:2024/04/29 05:43

需要在前台加上BulletedList控件,如果需要什么设置自己在前台改一下就行

string connStr = @"data source=.;initial catalog=Study_CSDN.net;user id=sa;password=123";//数据库连接字符串
                SqlConnection conn1 = new SqlConnection(connStr);
                conn1.Open();//打开连接
                SqlCommand cmd = conn1.CreateCommand();
                cmd.CommandText = "select * from top0 where(topDesc like'%" + Session["text"].ToString() + "%')";//这个是模糊查询
                DataSet ds = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
                cmd.ExecuteNonQuery();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string desc = ds.Tables[0].Rows[i]["topDesc"].ToString();
                    string url = ds.Tables[0].Rows[i]["topUrl"].ToString();
                    ListItem list = new ListItem(desc, url);
                    BulletedList1.Items.Add(list);
                }