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

后端 未结 13 2356
星月不相逢
星月不相逢 2020-11-28 01:35

How do create a DataTable in C#?

I did like this:

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


        
13条回答
  •  借酒劲吻你
    2020-11-28 01:52

    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.

    To export only the schema/structure, do:

    dt.WriteXMLSchema("dtSchemaOrStructure.xml");
    

    Additionally, you can also export your data:

    dt.WriteXML("dtDataxml");
    

提交回复
热议问题