【问题标题】:Manipulating DataSets操作数据集
【发布时间】:2017-02-11 15:17:59
【问题描述】:

我在操作/导航数据集时遇到问题,我将其作为自动生成的 XML 文件提取。我的目标是将某些节点放入 SQL 表的列中。

我的 XML

<root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:mix="http://www.jcp.org/jcr/mix/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:rep="internal" xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:sv="http://www.jcp.org/jcr/sv/1.0">
      <submission name="entry0">
            <entry name="firstName">Name</entry> 
            <entry name="School">The University of College</entry> 
            <entry name="ExpectedYearofEntry">2019</entry> 
            <entry name="mailingAddress">Some st</entry> 
      </submission>
      <submission name..........
</root>

我正在尝试这个:

string xmlFile = @"xml.xml";

DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);
DataTable table = dataSet.Tables["entry"];
IEnumerable<DataRow> row = table.AsEnumerable();

当我中断时,我可以看到行中的数据;也就是说,ItemArray0 包含 firstName 和 'Name'。但我不知道如何从行中提取这些数据。

【问题讨论】:

    标签: c# .net xml dataset


    【解决方案1】:

    您可以使用 SqlBulkCopy 之类的方法有效地将所有这些行插入到表中。但是,您必须为 DataSet 中的每个表执行此操作。

    很好的例子:https://msdn.microsoft.com/en-us/library/ex21zs8x(v=vs.110).aspx 一旦你有一个要插入的数据表。

    if (dt.Rows.Count > 0)
    {
        string consString = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(consString))
        {
            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            {
                //Set target table
                sqlBulkCopy.DestinationTableName = "dbo.Customers";
    
                //Map the DataTable columns with the database table
                sqlBulkCopy.ColumnMappings.Add("Id", "CustomerId");
                sqlBulkCopy.ColumnMappings.Add("Name", "Name");
                sqlBulkCopy.ColumnMappings.Add("Country", "Country");
                con.Open();
                sqlBulkCopy.WriteToServer(dt);
                con.Close();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多