[英]Getting one row from a Strongly Typed dataset without getting who table
I currently have this code: 我目前有以下代码:
/// <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;
}
}
Which is pretty terrible in performance. 这在性能上非常糟糕。 It grabs and loads a large table everytime someone wants one row.
每当有人想要一行时,它都会抓取并加载一张大桌子。 There has to be a better way.
一定有更好的方法。
How can I select one row from a table, without having to fill an entire table and still keep the strong data typing? 如何从表中选择一行,而不必填写整个表并仍然保持强数据键入?
There is. 有。 Associate a query with your
TableAdapter
. 将查询与
TableAdapter
关联。 TableAdapter Overview and mainly How to: Create TableAdapter Queries . TableAdapter概述 ,主要是如何:创建TableAdapter查询 。
Then you fill the dataset with the queried rows. 然后,用查询的行填充数据集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.