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);