c# 虚拟一个DataTable 并添加数据的方法分享


摘要:
下文分享c#生成一个DataTable方法,如下所示:

---定义DataTable
    DataTable  tbMaoMao = new DataTable("maomao");
---定义一个数据列,并设置为自增长列

DataColumn tbColumn = null;
tbColumn = tbMaoMao.Columns.Add("keyId", Type.GetType("System.Int32"));
tbColumn.AutoIncrement = true;   //自增加列
tbColumn.AutoIncrementSeed = 1;//起始为1
tbColumn.AutoIncrementStep = 1;//步长为1
tbColumn.AllowDBNull = false; //禁止列值为null



dc = tbMaoMao.Columns.Add("info", Type.GetType("System.String"));     

DataRow tbRow; //定义一行
tbRow = tbMaoMao.NewRow();
tbRow["info"] = "sql爱好者"; 
tbMaoMao.Rows.Add(tbRow);

tbRow = tbMaoMao.NewRow();
tbRow["info"] = "猫猫小屋"; 
tbMaoMao.Rows.Add(tbRow);
----给dataTable添加数据行方法2 

tbMaoMao.Rows.Add(new object[]{null,"sqlserver"});
tbMaoMao.Rows.Add(new object[] { null, "sqlblogs"});