一:DataSet
1 DataSet ds = new DataSet();//创建DataSet 2 DataTable dt = new DataTable();//创建一个DataTalbe 3 4 dt.Columns.Add(new DataColumn("id", typeof(int)));//为dt_dry表内建立Column 5 dt.Columns.Add(new DataColumn("SQBM", typeof(string))); 6 dt.Columns.Add(new DataColumn("UNAME", typeof(string))); 7 8 for (int i = 0; i < ods.Tables[0].Rows.Count; i++) 9 { 10 DataRow dr = dt.NewRow(); 11 12 dr["id"] = i + 1; 13 dr["SQBM"] = ods.Tables[0].Rows[i]["SQBM"].ToString(); 14 dr["UNAME"] = username; 15 if (i == ods.Tables[0].Rows.Count - 1) 16 { 17 dt.Rows.Add(dr); 18 ds.Tables.Add(dt); 19 } 20 else 21 dt.Rows.Add(dr); 22 } 23 }
二:DataTable
1 DataTable dt = new DataTable(); 2 3 dt.Columns.Add(new DataColumn("id", typeof(string))); 4 dt.Columns.Add(new DataColumn("UName", typeof(string))); 5 6 for (Int i = 0; i < ds.Table[0].Rows.count; i++) 7 { 8 DataRow dr = dt.NewRow(); 9 dr["id"] = ds.Table[0].Rows[i]["User_ID"].tostring(); 10 dr["UName"] = ds.Table[0].Rows[i]["strUName"].tostring(); 11 dt.Rows.Add(dr); 12 13 }
时间: 2024-11-15 00:25:21