public static class DbHelper { public static ListGetData (string sql) { var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ToString()); connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = sql; command.CommandType = CommandType.Text; var reader = command.ExecuteReader(); var list = GetList (reader); command.Connection.Close(); return list; } private static List GetList (SqlDataReader reader) { var list = new List (); while (reader.Read()) { list.Add(Get (reader)); } reader.Close(); reader.Dispose(); return list; } private static T Get (SqlDataReader reader) { var t = Activator.CreateInstance (); var type = typeof(T); foreach (var field in type.GetProperties()) { var value = reader[field.Name] is DBNull ? null : reader[field.Name]; field.SetValue(t, value); } return t; } }