C#手動建立Table後,並設置列和賦值 public DataTable CreateTable() { //創建Table1 DataTable dt = new DataTable(); //對Table1添加列名,並設置列值類型 DataTable dt1 = new DataTable() ...
C#手動建立Table後,並設置列和賦值
public DataTable CreateTable() { //創建Table1 DataTable dt = new DataTable(); //對Table1添加列名,並設置列值類型 DataTable dt1 = new DataTable();//創建Table1 dt1.Columns.Add("ID", typeof(string)); dt1.Columns.Add("NAME", typeof(string)); dt1.Columns.Add("AGE", typeof(int)); dt1.Columns.Add("SEX", typeof(string)); //對Table1進行設置KEY dt1.PrimaryKey = new DataColumn[] { dt1.Columns["ID"] }; //對Tabel添加數值 for (int i = 0; i < 2; i++) { DataRow dr = dt1.NewRow(); dr["ID"] = "00" + i.ToString(); dr["NAME"] = "00-" + i.ToString(); dr["AGE"] = 15 + i; dr["SEX"] = "M"; dt1.Rows.Add(dr); } return dt1; }