新建DataTable

时间:2024-03-24 13:36:50
//创建DataTable
DataTable dt = new DataTable("NewDt"); //创建自增长的ID列
DataColumn dc = dt.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true; //自动增加
dc.AutoIncrementSeed = ; //起始为1
dc.AutoIncrementStep = ; //步长为1
dc.AllowDBNull = false; //非空 //创建其它列表
dt.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Age", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Score", Type.GetType("System.Decimal")));
dt.Columns.Add(new DataColumn("CreateTime", Type.GetType("System.DateTime"))); //创建数据
DataRow dr = dt.NewRow();
dr["Name"] = "张三";
dr["Age"] = ;
dr["Score"] = 85.5;
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["Name"] = "李四";
dr["Age"] = ;
dr["Score"] = ;
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["Name"] = "王五";
dr["Age"] = ;
dr["Score"] = 63.5;
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr);

新建DataTable