简体   繁体   English

使用数据集C#从xml文件填充SQL

[英]Populate SQL from xml file using dataset, c#

I have a big xml file, from which I get many tables: 我有一个很大的xml文件,从中可以得到很多表:

DataSet l_DataSet = new DataSet();
l_DataSet.ReadXml(@"D:\file.xml");

tables that I have in dataset: 我在数据集中的表格:

Channels: id_table, id_channel, name* * *. 频道:id_table,id_channel,名称* * *。
Links: id_table, id_channel, url. 链接:id_table,id_channel,URL。
Countries: id_table, id_channel, country. 国家:id_table,id_channel,国家/地区。

Now I have to merge them in some way to insert into 1 table: 现在,我必须以某种方式将它们合并以插入一张表中:

SQLtable: id_channel, code, description (may be null), url, country. SQLtable:id_channel, 代码,描述 (可以为null),URL,国家/地区。

*The main problem is: **Code and description is a "name" value from Channels table, which looks like: *主要问题是:**代码和描述是 Channels表中的“名称”值 ,如下所示:
id_table id_channel code/description id_table id_channel代码/说明
1 1 code1 1 1代码1
2 1 description1 2 1说明1
3 2 code2 3 2代码2
4 2 description2 4 2说明2
5 3 code3 5 3代码3
6 4 code4 6 4代码4
7 4 description4 7 4说明4
sometimes there is description missing. 有时缺少描述。

I don't know how can I merge tables in the dataset in c#? 我不知道如何在C#中合并数据集中的表? Some directions? 一些方向?

Thank you! 谢谢!

If I knew more about your table structure and XML document I can try and give you a better example 如果我对您的表结构和XML文档有更多了解,我可以尝试为您提供更好的示例

Depending on how many rows you are inserting I would add the data in chunks to help not lock up the database. 根据要插入的行数,我将数据分块添加,以帮助不锁定数据库。 Typically I pass 25,000 chunks at a time. 通常,我一次传递25,000个块。 If your rows are more than 25000 then for loop through it only doing 25000 at a time. 如果您的行数超过25000,则for循环遍历它一次仅执行25000。

On the SQL Side create a procedure: 在SQL端创建一个过程:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[prI_BulkAddData](@Data Xml)
As

 -- Add Handle to XML Data
 Declare @xmlDataHandle Int
 Exec sp_xml_preparedocument @xmlDataHandle Output, @Data

 Insert Into TableName(Column1, Column2, Column3)
 Select ElementName1, ElementName2, ElementName3 From OpenXml(@xmlDataHandle, '/Root/Channel', 2)
 With(
    [ElementName1] int,
    [ElementName2] varchar(50,
    [ElementName3] datetime
     )

      -- Remove Handle to Free-Up Memory
 Exec sp_xml_removedocument @xmlDataHandle

GO

On your visual studio side Call the Procedure: 在您的Visual Studio一侧,调用以下过程:

EXEC prI_BulkAddData Data = 'PUT XML HERE'

This is just an example obvious you will need to write some C# code to read the XML file and to Execute a Stored Procedure (I typically use Linq to SQL as I am lazy and it works good and dont want to write a bunch of data code) 这只是一个显而易见的例子,您将需要编写一些C#代码来读取XML文件并执行存储过程(由于我很懒,我通常使用Linq来执行SQL,并且效果很好,并且不想编写一堆数据代码)

In the Stored Proc make sure you adjust it to how your XML doc is wrote. 确保在“存储过程”中将其调整为如何编写XML文档。

Here is an example I did once to parse large XML files: 这是我曾经解析大型XML文件的示例:

  for (int t = 0; t < data.Data.Count(); t = t + 25000)
                            {
                                var xEle = new XElement("DataCollections",
                                        from emp in data.Data.Skip(t).Take(25000)
                                        select new XElement("DataCollection",
                                                   new XElement("AssetID", emp.AssetID),
                                                   new XElement("DataPointID", emp.DataPointID),
                                                   new XElement("SourceTag", emp.SourceTag),
                                                   new XElement("TimeStep", emp.TimeStep),
                                                   new XElement("RetInterval", emp.RetInterval),
                                                   new XElement("DatapointDate", emp.DatapointDate.ToString()),
                                                   new XElement("DataPointValue", emp.DataPointValue),
                                                   new XElement("DataFlagID", emp.DataFlagID),
                                                   new XElement("DateDataGrabbed", emp.DateDataGrabbed.ToString()),
                                                   new XElement("DateAddedToDB", emp.DateAddedToDB.ToString())
                                        ));

                                _DataCollectorManager.Services.dataProcessService.BulkAddPIData(xEle);

                            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM