public DataSet BulidDataSet()
{
DataSet ds=new DataSet();
DataTable dt = new DataTable();
DataColumn col = new DataColumn("姓名",typeof(string));
col.MaxLength = 20;
dt.Columns.Add(col); //注意是dt.columns 然後ADD
DataColumn col1 = new DataColumn("年齡", typeof(int));
col.MaxLength = 20;
dt.Columns.Add(col1);
DataRow rw =dt.NewRow(); //給DT增加一個新行對象
rw["姓名"] = "aking";
rw["年齡"] = 26;
dt.Rows.Add(rw); // dt.Rows.Add(dt.NewRow()); 增加一個空
DataRow rw1 = dt.NewRow();
rw1["姓名"] = "yao";
rw1["年齡"] = 26;
dt.Rows.Add(rw1);
DataRow rw2 = dt.NewRow();
rw2["姓名"] = "Ayao";
rw2["年齡"] = 26;
dt.Rows.Add(rw2);
ds.Tables.Add(dt);
return ds;
}
更有邏輯性如下
DataSet ret=new DataSet();
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("ID",Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Name",Type.GetType("System.Strig")));
//到這一個dt已經建完
//下面就要添加數據
DataRow rw1 = dt.NewRow();
rw1["ID"] = "yao"; rw1["Name"] = "aking"; dt.Rows.Add(rw1);