如何在C#中创建DataTable?
我喜欢这样:
DataTable dt = new DataTable(); dt.clear(); dt.Columns.Add("Name"); dt.Columns.Add("Marks");
如何查看DataTable的结构?
现在我想添加ravi for Name
和500 for Marks
.我怎样才能做到这一点?
这是代码:
DataTable dt = new DataTable(); dt.Clear(); dt.Columns.Add("Name"); dt.Columns.Add("Marks"); DataRow _ravi = dt.NewRow(); _ravi["Name"] = "ravi"; _ravi["Marks"] = "500"; dt.Rows.Add(_ravi);
要查看结构,或者我将其重新表示为模式,可以通过执行以下操作将其导出到XML文件.
要仅导出架构/结构,请执行以下操作:
dt.WriteXMLSchema("dtSchemaOrStructure.xml");
此外,您还可以导出数据:
dt.WriteXML("dtDataxml");
您也可以传入一个对象数组,如下所示:
DataTable dt = new DataTable(); dt.Clear(); dt.Columns.Add("Name"); dt.Columns.Add("Marks"); object[] o = { "Ravi", 500 }; dt.Rows.Add(o);
甚至:
dt.Rows.Add(new object[] { "Ravi", 500 });
// Create a DataTable and add two Columns to it DataTable dt=new DataTable(); dt.Columns.Add("Name",typeof(string)); dt.Columns.Add("Age",typeof(int)); // Create a DataRow, add Name and Age data, and add to the DataTable DataRow dr=dt.NewRow(); dr["Name"]="Mohammad"; // or dr[0]="Mohammad"; dr["Age"]=24; // or dr[1]=24; dt.Rows.Add(dr); // Create another DataRow, add Name and Age data, and add to the DataTable dr=dt.NewRow(); dr["Name"]="Shahnawaz"; // or dr[0]="Shahnawaz"; dr["Age"]=24; // or dr[1]=24; dt.Rows.Add(dr); // DataBind to your UI control, if necessary (a GridView, in this example) GridView1.DataSource=dt; GridView1.DataBind();
要添加一行:
DataRow row = dt.NewRow(); row["Name"] = "Ravi"; row["Marks"] = 500; dt.Rows.Add(row);
要查看结构:
Table.Columns
创建DataTable:
DataTable MyTable = new DataTable(); // 1 DataTable MyTableByName = new DataTable("MyTableName"); // 2
向表中添加列:
MyTable.Columns.Add("Id", typeof(int)); MyTable.Columns.Add("Name", typeof(string));
向DataTable方法1添加行:
DataRow row = MyTable.NewRow(); row["Id"] = 1; row["Name"] = "John"; MyTable.Rows.Add(row);
向DataTable方法2添加行:
MyTable.Rows.Add(2, "Ivan");
向DataTable方法3添加行(通过相同结构从另一个表添加行):
MyTable.ImportRow(MyTableByName.Rows[0]);
向DataTable方法4添加行(从另一个表添加行):
MyTable.Rows.Add(MyTable2.Rows[0]["Id"], MyTable2.Rows[0]["Name"]);
向DataTable方法5添加行(在索引处插入行):
MyTable.Rows.InsertAt(row, 8);
您可以使用DataRow.Add(params object [] values)而不是四行来编写一个衬管.
dt.Rows.Add("Ravi", "500");
在创建新DataTable
对象时,似乎没有必要Clear
DataTable
在下一个语句中.您还可以使用DataTable.Columns.AddRange
on语句添加列.完整的代码将是.
DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("Marks") }); dt.Rows.Add("Ravi", "500");
您可以在一行中添加Row
DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patient", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); // Here we add five DataRows. table.Rows.Add(25, "Indocin", "David", DateTime.Now); table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
DataTable dt=new DataTable(); Datacolumn Name = new DataColumn("Name"); Name.DataType= typeoff(string); Name.AllowDBNull=false; //set as null or not the default is true i.e null Name.MaxLength=20; //sets the length the default is -1 which is max(no limit) dt.Columns.Add(Name); Datacolumn Age = new DataColumn("Age", typeoff(int));` dt.Columns.Add(Age); DataRow dr=dt.NewRow(); dr["Name"]="Mohammad Adem"; // or dr[0]="Mohammad Adem"; dr["Age"]=33; // or dr[1]=33; dt.add.rows(dr); dr=dt.NewRow(); dr["Name"]="Zahara"; // or dr[0]="Zahara"; dr["Age"]=22; // or dr[1]=22; dt.rows.add(dr); Gv.DataSource=dt; Gv.DataBind();
为此,您必须将数据行添加到数据表中.
// Creates a new DataRow with the same schema as the table. DataRow dr = dt.NewRow(); // Fill the values dr["Name"] = "Name"; dr["Marks"] = "Marks"; // Add the row to the rows collection dt.Rows.Add ( dr );
DataTable dt=new DataTable(); DataColumn Name = new DataColumn("Name",typeof(string)); dt.Columns.Add(Name); DataColumn Age = new DataColumn("Age", typeof(int));` dt.Columns.Add(Age); DataRow dr=dt.NewRow(); dr["Name"]="Kavitha Reddy"; dr["Age"]=24; dt.add.Rows(dr); dr=dt.NewRow(); dr["Name"]="Kiran Reddy"; dr["Age"]=23; dt.Rows.add(dr); Gv.DataSource=dt; Gv.DataBind();
最简单的方法是创建一个DtaTable
DataTable table = new DataTable { Columns = { "Name", // typeof(string) is implied {"Marks", typeof(int)} }, TableName = "MarksTable" //optional }; table.Rows.Add("ravi", 500);