[5287][C#][技巧]由Datatable查詢符合條件資料
使用SQL組合where查詢條件很容易,如select X from A where B = C,
那使用datatable呢?
假如已有DataTable dt
Column 1: string,"ARR_AREA_CD"
Column 2: int,"SORT_SQ"
Column 3: string, "ARR_CITY_CD"
可使用以下方法,以column 1 & column 2為條件,查詢出column 3的值:
dt.PrimaryKey = new DataColumn[] { dt.Columns["ARR_AREA_CD"], dt.Columns["SORT_SQ"] };
object[] search = new object[] { "003", 1 };
DataRow dr = dt.Rows.Find(search);
if (dr == null)
{
txt001_1.Text = "";
}
else
{
txt001_1.Text = dr["ARR_CITY_CD"].ToString();
}