C#:DataSet與DataTable的使用
阿新 • • 發佈:2018-12-27
DataSet 中可以有多個 DataTable;DataTable 中有多個DataColumn (列名),多個Rows (資料行)。
DataSet data = new DataSet("資料集"); DataTable dataTable1 = new DataTable("資料表1"); //DataTable dataTable2 = new DataTable("資料表2"); data.Tables.AddRange(new DataTable[] { dataTable1 }); //data.Tables.Add(dataTable2); DataColumn age = new DataColumn("年齡",typeof(int)); DataColumn name = new DataColumn("姓名",typeof(String)); DataColumn hobby = new DataColumn("愛好",typeof(String)); dataTable1.Columns.AddRange(new DataColumn[] { age, name, hobby }); dataTable1.Rows.Add(18,"liuyan樣","運動"); dataTable1.Rows.Add(13, "sdg樣", "健身"); dataTable1.Rows.Add(17, "asfagsw樣", "跑步"); //遍歷DataSet中的資料 foreach (DataTable dt in data.Tables) { foreach(DataRow row in dt.Rows) { String myage = row[0].ToString(); String myname = row[1].ToString(); String myhobby = row[2].ToString(); } }