1 public IList GetModelList(string tablename, string where) 2 { 3 IList list = null; 4 DataTable dataTable = new DataTable(); 5 string sql = "select * from " + tablename; 6 if (where != "") 7 { 8 sql += " Where " + where; 9 }10 try11 {12 System.Data.SqlClient.SqlDataReader dr = DbHelperSQL.ExcuteReader(sql); 13 for (int i = 0; i < dr.FieldCount; i++)14 {15 DataColumn mydc = new DataColumn(); //关键的一步16 mydc.DataType = dr.GetFieldType(i);17 mydc.ColumnName = dr.GetName(i);18 dataTable.Columns.Add(mydc); //关键的第二步19 }20 while (dr.Read())21 {22 DataRow mydr = dataTable.NewRow(); //关键的第三步23 for (int i = 0; i < dr.FieldCount; i++)24 {25 mydr[i] = dr[i].ToString();26 }27 dataTable.Rows.Add(mydr); //关键的第四步28 mydr = null;29 }30 dr.Close();31 list = dataTable as IList;32 return (list);33 34 }35 catch (Exception ex)36 {37 throw ex;38 } 39 }