2

I have a datatable which has the following structure

DataColumn sourceTblCol = new DataColumn("SourceTableName", typeof(System.Data.SqlTypes.SqlString));
                    importXMLMainDt.Columns.Add(sourceTblCol);
                    DataColumn ordinalPosCol = new DataColumn("OrdinalPosition", typeof(System.Data.SqlTypes.SqlInt32));
                    importXMLMainDt.Columns.Add(ordinalPosCol);
                    DataColumn grpNameCol = new DataColumn("GroupName", typeof(System.Data.SqlTypes.SqlString));
                    importXMLMainDt.Columns.Add(grpNameCol);
                    DataColumn entityTypeIDCol = new DataColumn("EntityTypeID", typeof(System.Data.SqlTypes.SqlInt32));
                    importXMLMainDt.Columns.Add(entityTypeIDCol);
                    DataColumn entityNameCol = new DataColumn("EntityName", typeof(System.Data.SqlTypes.SqlString));
                    importXMLMainDt.Columns.Add(entityNameCol);
                    DataColumn exportXMLCol = new DataColumn("ExportData", typeof(System.Data.SqlTypes.SqlXml));
                    importXMLMainDt.Columns.Add(exportXMLCol);

I am looping through all the xml files in a directory and adding that XML content as sqlXML in the datatable

xmlFiles = Directory.GetFiles(this.WrkImportFilePath, "*.xml");
                    foreach (string filename in xmlFiles)
                    {

                                using (XmlReader xmlReaderObj = XmlReader.Create(filename))
                                {
                                    while (xmlReaderObj.Read())
                                    {
                                        sqlxmlObj = new SqlXml(xmlReaderObj);
                                    }
                                    xmlReaderObj.Close();
                                }
                                importXMLMainDt.Rows.Add(srcTableName, ordinalPos, grpName, entityTypeID, string.Empty, sqlxmlObj);
                                sqlxmlObj = null;

                        }
                    }

It is working fine for 2500 XML files but throwing exception if the number of file goes more that 3000.The max number of files can be upto 12,000. I need to create this datatable and pass it as table valued parameter in stored procedure.So how do I handle this problem?

1
  • Why isn't inserting them individually an option, that is, just call the stored procedure more than once? Even if you do get this to work with all documents, SQL Server would be forced to allocate a huge amount of memory as well, not to mention a big chunk of transaction log. Is it a business requirement that all the inserts happen transactionally? Commented May 11, 2016 at 13:37

1 Answer 1

2

You get an out-of-memory-exception because you try to load all your files in one go and then send it to the database in one go. The machine's memory is just not big enough...

You have several options:

  1. Buy huge amounts of RAM :-)
  2. Transfer the data in parts (one-by-one ?)
  3. If the XML files reside in a location you can reach from SQL Server directly, you might - much easier! - push the file names only and load the XML into a DB-table's column via TSQL.

Just in case you want to try option 3:

Try to load one of your files. If this works, it should work with all of them...

DECLARE @yourXML AS XML=
(
SELECT CONVERT(XML, BulkColumn,2) AS BulkColumn
FROM OPENROWSET(BULK 'PathToFile.xml', SINGLE_BLOB) AS x
);
SELECT @yourXML;
Sign up to request clarification or add additional context in comments.

1 Comment

Option 2 solved my problem thanks option 3 might not work due to filepath permission as DB server and application server are different.Also in 64 bit OS this problem is not happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.