1

我有大型 XML 文件(2 Gb),我需要将数据导入 sql ce db。XML 文件有一个根标签和许多具有 20 个属性的相同标签(如 2d 表):

<Objects>
<Object ID="" name="" level="" />
<Object ID="" name="" level="" /> 

为了将数据从 XML 加载到我使用的 sql ce db SqlCeBulkCopy(在codeplex上)。我尝试使用DataTable,但出现此错误(因为 XML 大于 2 Gb):

“System.OutOfMemoryException”

我用来XmlReader从 XML 文件中读取信息:

XmlReader r = XmlReader.Create("file:////" + PathToFile);
  while (r.Read())
    {
        if ((r.Name == "Object") && (r.HasAttributes))
        {

        }
    }

在这种情况下如何使用IDataReaderwith XmlReaderfor SqlCeBulkCopy(任何例子)?

4

1 回答 1

1

xmlReader and DbDataReader are not related. But SqlCeBulkcopy supports both List and DataTable objects as well as DbDataReader

Use a List, and only load say 10000 rows/records, then bulk copy those and continue. You cannot fit a 2 GB XML file into memory!

So outside your while loop add (pseudo code):

var list = new List<MyObjects>();

And inside your while loop add:

var myObject = new MyObject();
myObject.Property1 = value from xmlreader;
myObject.Property2 = anotherValue from xmlreader;

list.Add(myObject);

if (list.Count == 10000)
{
   //run BulkCopy
   list.Clear();
} 
于 2015-04-10T12:23:25.887 回答