怎样在listview中显示数据库中的数据?请给我指点下;散分。。 

解决方案 »

  1.   

       listView1.Items.Clear();     //建立数据连接
       SqlConnection conn = new SqlConnection(sqlstr);
       conn.Open(); 
       
       //打开数据集
       string strCom = "select * from table";
       SqlCommand cmd = new SqlCommand(strCom,conn);
       SqlDataReader r = cmd .ExecuteReader();   //对数据集中的数据记录进行遍历,在遍历中添加记录到列表中
       while ( r.Read ( ) ) 
       { 
        ListViewItem li = new ListViewItem( new string[] {r["USERID"].ToString ( ), r["USERNAME"].ToString ( ),r["PASSWORD"].ToString ( ),r["POLICESTATIONID"].ToString ( ), r["ROLEID"].ToString ( ) , r["IP"].ToString ( ), r["DESCRIPTION"].ToString ( ), r["ISDELETE"].ToString ( ), r["CHINESENAME"].ToString ( ) } );
         listView1.Items.Add ( li ) ; 
       }    //关闭数据集、关闭数据连接
       r.Close();
       conn.Close();
      

  2.   

    比方说 select name,age from userinfo 
    用read循环读取
    listviewitem item1=new listviewitem("item1",0);
    item1.text=dr.getstring(0); 获得name
    item1.text=dt.getint32(1).tostring(); 获得年龄
    listview.items.add(item1);
    用datatable循环读取
    for(int i=0;i<dt.rows.count;i++)
    listviewitem item1=new listviewitem("item1",0);
    item1.text=dt.rows[i]["name"].tostring();""里要和数据库字段一样
    item1.text=dt.rows[i]["age"].tostring();
    listview.items.add(item1);