![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
SqlConnection conn = BaseClass.DBConn.CjCon();//创建一个SQL SERVER连接对象。
try
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select xh as 学号,xm from xsb", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.listView1.Columns.Add("xh", 80, HorizontalAlignment.Center);
this.listView1.Columns.Add("姓名", 60, HorizontalAlignment.Center);//增加列标题
listView1.View = View.Details;
listView1.GridLines = true;//显示网格线。。
string[] str = new string[2];
foreach (DataRow row in ds.Tables[0].Rows)
{
str[0] = row["学号"].ToString();
str[1] = row["xm"].ToString();
ListViewItem item = new ListViewItem(str, 0);
listView1.Items.Add(item);
}
}
catch (Exception er)
{
MessageBox.Show(er.ToString());
conn.Close();
}