代码之家  ›  专栏  ›  技术社区  ›  Malfist

从强类型数据集中获取一行而不获取who表

  •  0
  • Malfist  · 技术社区  · 16 年前

    我现在有这个代码:

       /// <summary>
        /// This is an ineffecient method of getting a tenant by their id.
        /// </summary>
        /// <param name="id">int ID of the tenant to be selected</param>
        /// <returns>Tenant with the specific ID, or null if not found.</returns>
        public static Tenant GetTenantByID(int id){
            RentalEaseDataSetTableAdapters.tblTenantTableAdapter adapter = new RentalEaseLogic.Database.RentalEaseDataSetTableAdapters.tblTenantTableAdapter();
            RentalEaseDataSet ds = new RentalEaseDataSet();
    
            adapter.Fill(ds.tblTenant);
    
            DataRow[] rows = ds.tblTenant.Select("ID = " + id);
    
            if (rows.Length > 0) {
                return new Tenant(rows[0] as RentalEaseDataSet.tblTenantRow);
            } else {
                return null;
            }
        }
    

    这在性能上相当糟糕。每当有人想要一行,它就会抓取并加载一个大表。必须有更好的方法。

    如何从一个表中选择一行,而不必填充整个表,并且仍然保持强数据类型?

    1 回复  |  直到 16 年前
        1
  •  1
  •   Jaroslav Jandek    16 年前

    有。将查询与 TableAdapter . TableAdapter Overview 主要 How to: Create TableAdapter Queries .

    然后用查询的行填充数据集。