C# 向DataTable中插入数据或伪造DataTable

    1. 方法一:  
    2.   
    3. DataTable  tblDatas = new DataTable("Datas");  
    4. DataColumn dc = null;  
    5. dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));  
    6. dc.AutoIncrement = true;//自动增加  
    7. dc.AutoIncrementSeed = 1;//起始为1  
    8. dc.AutoIncrementStep = 1;//步长为1  
    9. dc.AllowDBNull = false;//  
    10.   
    11. dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));  
    12. dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));  
    13. dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));  
    14.   
    15. DataRow newRow;  
    16. newRow = tblDatas.NewRow();  
    17. newRow["Product"] = "水果刀";  
    18. newRow["Version"] = "2.0";  
    19. newRow["Description"] = "打架专用";  
    20. tblDatas.Rows.Add(newRow);  
    21.   
    22. newRow = tblDatas.NewRow();  
    23. newRow["Product"] = "折叠凳";  
    24. newRow["Version"] = "3.0";  
    25. newRow["Description"] = "行走江湖七武器之一";  
    26. tblDatas.Rows.Add(newRow);  
    27.   
    28. 方法二:  
    29.   
    30.  DataTable tblDatas = new DataTable("Datas");  
    31. tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));  
    32. tblDatas.Columns[0].AutoIncrement = true;  
    33. tblDatas.Columns[0].AutoIncrementSeed = 1;  
    34. tblDatas.Columns[0].AutoIncrementStep = 1;  
    35.   
    36. tblDatas.Columns.Add("Product", Type.GetType("System.String"));  
    37. tblDatas.Columns.Add("Version", Type.GetType("System.String"));  
    38. tblDatas.Columns.Add("Description", Type.GetType("System.String"));  
    39.   
    40. tblDatas.Rows.Add(new object[]{null,"a","b","c"});  
    41. tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });  
    42. tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });  
    43. tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });  
    44. tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); 
原文地址:https://www.cnblogs.com/shouyeren/p/6639351.html