[英]Getting one row from a Strongly Typed dataset without getting who table
我目前有以下代码:
/// <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;
}
}
这在性能上非常糟糕。 每当有人想要一行时,它都会抓取并加载一张大桌子。 一定有更好的方法。
如何从表中选择一行,而不必填写整个表并仍然保持强数据键入?
有。 将查询与TableAdapter
关联。 TableAdapter概述 ,主要是如何:创建TableAdapter查询 。
然后,用查询的行填充数据集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.