读入数据到list,哪里不对
20004 2011-01-16 02:14:49 using (SqlConnection con = new SqlConnection(ConnectString))
{
try
{
DataSet ds = new DataSet();
SqlCommand cmd=new SqlCommand (sql,con);
SqlDataReader dr = cmd.ExecuteReader();
int nFields = dr.FieldCount;
lv.Clear();
for (int i = 0; i < nFields ; i++)
{
lv.Columns.Add(dr.GetName(i), 100, HorizontalAlignment.Left);
}
int nRow = 0;
while (dr.Read())
{
// Create an array of subitems for quick insertion
// The subitems will be all fields in the row except for
// the first field
String[] subitems = new String[nFields];
for (int i = 0; i < nFields; i++)
{
subitems[i] = dr[i].ToString();
}
// Insert a new item into the listview, and add the subitems at
// the same time. The item will be the first field in the row
ListViewItem item = new ListViewItem(subitems, -1);
lv.Items.Add(item);
++nRow;
}
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}