DataTable与DataSet常用操作实例
//创建DataSet
DataSet ds = new DataSet();
//创建DataTable
DataTable dt = new DataTable();
dt.Columns.Add("id",Type.GetType("System.Int32"));
dt.Columns["id"].AutoIncrement = true;
dt.Columns.Add("name",Type.GetType("System.String"));
//插入行
DataRow dw1 = dt.NewRow();
dw1["name"] = "test1";
dt.Rows.Add(dw1);
DataRow dw2 = dt.NewRow();
dw2["name"] = "test2";
dt.Rows.InsertAt(dw2,0);
//将DataTable添加到DataSet中
ds.Tables.Add(dt);
//DataTable中查询
DataTable dt = new DataTable();
DataRow dr[] = dt.Select("1 = 1");
//DataTable更新
DataTable dt = (DataTable)HttpContext.Current.Cache["MYCACHE"];
DataRow[] dr = dt.Select("1 = 1");
if (dr.Length > 0)
{
dr[0]["colName"] = "colValue";
}
//统计
object o = dt.Compute("SUM(col_name)", "1=1");