//连接字符串
string str = "Data Source=.;Initial Catalog=mu;User ID=sa;Password=111";
//创建数据库连接
SqlConnection con =new SqlConnection(str);
//打开数据库
con.Open();
//创建sql语句
string sql = "select studentName,addres,borndate from student";
//创建command命令
SqlCommand com = new SqlCommand(sql,con);
//执行sql语句
SqlDataReader dr = com.ExecuteReader();
while(dr.Read()){
string name = dr["studentName"].ToString();
string addres = dr["addres"].ToString();
string date = dr["borndate"].ToString();
//创建listView对象
ListViewItem item = new ListViewItem(name);
item.SubItems.Add(addres);
item.SubItems.Add(date);
listView1.Items.Add(item);
}
//关闭数据库
con.Close();
转载于:https://www.cnblogs.com/mumu555/p/6539301.html