using (SqlConnection conn = new SqlConnection(ConStr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;//记得要初始化connection
= "select 供应商ID,公司名称,联系人姓名,联系人职务,地址 from 供应商";
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
//List<供应商> cla = new List<供应商>();
while (reader.Read())
{
ListViewItem lvitem = new ListViewItem();
lvitem.SubItems[0].Text= reader.GetInt32(0).ToString();//供应商ID
lvitem.SubItems.Add(reader.GetString(1));//公司名称
lvitem.SubItems.Add(reader.GetString(2));//联系人姓名
lvitem.SubItems.Add(reader.GetString(3));/联系人职务
if (reader.IsDBNull(4))//判断是否为空值
{
lvitem.SubItems.Add("NULL");//地址
}
else
{
lvitem.SubItems.Add(reader.GetString(4));//地址
}
this.listView1.Items.Add(lvitem);