1

C# with .net 2.0 with a SQL server 2005 DB backend.

I've a bunch of XML files which contain data along the lines of the following, the structure varies a little but is more or less as follows:

<TankAdvisory>
  <WarningType name="Tank Overflow">
    <ValidIn>All current tanks</ValidIn> 
    <Warning>Tank is close to capacity</Warning> 
    <IssueTime Issue-time="2011-02-11T10:00:00" /> 
    <ValidFrom ValidFrom-time="2011-01-11T13:00:00" /> 
    <ValidTo ValidTo-time="2011-01-11T14:00:00" /> 
  </WarningType>
</TankAdvisory>

I have a single DB table that has all the above fields ready to be filled.

When I use the following method of reading the data from the XML file:

DataSet reportData = new DataSet();
reportData.ReadXml("../File.xml");

It successfully populates the Dataset but with multiple tables. So when I come to use SQLBulkCopy I can either save just one table this way:

sbc.WriteToServer(reportData.Tables[0]);

Or if I loop through all the tables in the Dataset adding them it adds a new row in the Database, when in actuality they're all to be stored in the one row.

Then of course there's also the issue of columnmappings, I'm thinking that maybe SQLBulkCopy is the wrong way of doing this.

What I need to do is find a quick way of getting the data from that XML file into the Database under the relevant columns in the DB.

4 Answers 4

1

Ok, so the original question is a little old, but i have just came across a way to resolve this issue.

All you need to do is loop through all the DataTables that are in your DataSet and add them to the One DataTable that has all the columns in the Table in your DB like so...

DataTable dataTable = reportData.Tables[0];

//Second DataTable
DataTable dtSecond = reportData.Tables[1];

foreach (DataColumn myCol in dtSecond.Columns)
{

    sbc.ColumnMappings.Add(myCol.ColumnName, myCol.ColumnName);
    dataTable.Columns.Add(myCol.ColumnName);
    dataTable.Rows[0][myCol.ColumnName] = dtSecond.Rows[0][myCol];
}

//Finally Perform the BulkCopy

sbc.WriteToServer(dataTable);
Sign up to request clarification or add additional context in comments.

Comments

1
foreach (DataColumn myCol in dtSecond.Columns)
{
    dataTable.Columns.Add(myCol.ColumnName);
    for (int intRowcnt = 0; intRowcnt <= dtSecond.Rows.Count - 1; intRowcnt++)
    {
        dataTable.Rows[intRowcnt][myCol.ColumnName] = dtSecond.Rows[intRowcnt][myCol];
    }
}

Comments

0

SqlBulkCopy is for many inserts. It's perfect for those cases when you would otherwise generate a lot of INSERT statements and juggle the limit on total number of parameters per batch. The thing about the SqlBulkCopy class though, is that it's a cranky. Unless you fully specify all column mappings for the data set it will throw an exception.

I'm assuming that your data is quite manageable since your reading it into a DataSet. If you where to have even larger data sets you could lift chunks into memory and then flush them to the database piece by piece. But if everything fits in one go, it's as simple as that.

The SqlBulkCopy is the fastest way to put data into the database. Just setup column mappings for all the columns, otherwise it won't work.

2 Comments

Thanks for the reply but I'm not sure this addresses my issue. THe bulkcopy works fine when the dataset contains only one table, I can map the columns and it all works ok. The problem arises with the xml sections like 'IssueTime' which get added to the Dataset as different tables and end up being added to the db as distinct rows of info.
Each SqlBulkCopy has one destination table. If you have a DataSet you use a SqlBulkCopy instance for each destination table in that DataSet.
0

Why reinvent the wheel? Use SSIS. Read with an XML Source, transform with one of the many Transformations, then load it with an OLE Db Destination into the SQL Server table. You will never beat SSIS in terms of runtime, speed to deploy the solution, maintenance, error handling etc etc.

2 Comments

Hmm, hadn't heard of SSIS, but that link is for a product that works with SQL server 2008, is there a version for 2005?
Go to that link, click on Versions, select SQL Server 2005.

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.