如何在 C# 中创建数据表以及如何添加行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1042618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:49:37  来源:igfitidea点击:

How to create a DataTable in C# and how to add rows?

c#ado.netdatatable

提问by Cute

How do create a DataTable in C#?

如何在 C# 中创建数据表?

I did like this:

我是这样的:

 DataTable dt = new DataTable();
 dt.clear();
 dt.Columns.Add("Name");
 dt.Columns.Add("Marks");

How do I see the structure of DataTable?

如何查看DataTable的结构?

Now I want to add ravifor Nameand 500for Marks. How can I do this?

现在我想添加raviforName500for Marks. 我怎样才能做到这一点?

回答by djdd87

To add a row:

添加一行:

DataRow row = dt.NewRow();
row["Name"] = "Ravi";
row["Marks"] = 500;
dt.Rows.Add(row);

To see the structure:

查看结构:

Table.Columns

回答by rahul

You have to add datarows to your datatable for this.

为此,您必须将数据行添加到数据表中。

// 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 );

回答by this. __curious_geek

Here's the code:

这是代码:

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);

To see the structure, or rather I'd rephrase it as schema, you can export it to an XML file by doing the following.

要查看结构,或者更确切地说,我将其重新表述为模式,您可以通过执行以下操作将其导出到 XML 文件。

To export only the schema/structure, do:

要仅导出模式/结构,请执行以下操作:

dt.WriteXMLSchema("dtSchemaOrStructure.xml");

Additionally, you can also export your data:

此外,您还可以导出数据:

dt.WriteXML("dtDataxml");

回答by Rune Grimstad

In addition to the other answers.

除了其他答案。

If you control the structure of the DataTable there is a shortcut for adding rows:

如果您控制 DataTable 的结构,则有一个添加行的快捷方式:

// Assume you have a data table defined as in your example named dt dt.Rows.Add("Name", "Marks");

// 假设你有一个数据表定义为你的例子中名为 dt dt.Rows.Add("Name", "Marks");

The DataRowCollection.Add() method has an overload that takes a param array of objects. This method lets you pass as many values as needed, but they must be in the same order as the columns are defined in the table.

DataRowCollection.Add() 方法有一个重载,它采用对象的 param 数组。此方法允许您根据需要传递任意数量的值,但它们的顺序必须与表中定义的列的顺序相同。

So while this is a convenient way to add row data, it can be risky to use. If the table structure changes your code will fail.

因此,虽然这是添加行数据的便捷方式,但使用起来可能存在风险。如果表结构发生变化,您的代码将失败。

回答by James McConnell

You can also pass in an object array as well, like so:

您也可以传入一个对象数组,如下所示:

DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
object[] o = { "Ravi", 500 };
dt.Rows.Add(o);

Or even:

甚至:

dt.Rows.Add(new object[] { "Ravi", 500 });

回答by shahnawaz

// 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();

回答by Mohammed Shafi Adem

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();

回答by kavitha Reddy

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();

回答by Adil

You can write one linerusing DataRow.Add(params object[] values)instead of four lines.

可以编写一个衬里使用DataRow.Add(params对象[]的值),而不是四条线。

dt.Rows.Add("Ravi", "500");

As you create new DataTableobject, there seems noneed to ClearDataTablein very next statement. You can also use DataTable.Columns.AddRangeto add columns with on statement. Complete code would be.

当你创建新的DataTable对象,似乎没有必要ClearDataTable在第二天发言。您还可以使用DataTable.Columns.AddRangeon 语句添加列。完整的代码将是。

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("Marks") });     
dt.Rows.Add("Ravi", "500");

回答by naveen

The easiest way is to create a DtaTable as of now

最简单的方法是创建一个 DtaTable 作为现在

DataTable table = new DataTable
{
    Columns = {
        "Name", // typeof(string) is implied
        {"Marks", typeof(int)}
    },
    TableName = "MarksTable" //optional
};
table.Rows.Add("ravi", 500);