代码如下:
DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Id", typeof(string)); DataColumn dc2 = new DataColumn("Name", typeof(string)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); DataRow dr = dt.NewRow(); dr["Id"] = "1004125120"; dr["Name"] = "丁鹏"; dt.Rows.Add(dr); DataRow dr2 = dt.NewRow(); dr2["Id"] = "1004125121"; dr2["Name"] = "张乐"; dt.Rows.Add(dr2); // 1. 类似sql的Where后面跟的条件的查询语句 like、in 也可以 DataRow[] drs = dt.Select("Name = ‘张乐‘ and Id in (‘1004125121‘,‘1004125120‘)");//返回DataRow[] // 2. 位DT设置主键,Find(主键值)查询 //PrimaryKey是DT的一个属性 类型是DataColumn[] dt.PrimaryKey = new DataColumn[] { dt.Columns["Id"] }; DataRow drSelect = dt.Rows.Find("1004125120");//返回DataRow
时间: 2024-10-25 03:32:40