As a database developer with a very small amount of programming experience, I’m currently attempting to develop a C# .NET console application to import XML files into a SQL Server database. Once the import is done, I need to create a ‘Response’ file to be passed back to another application.
In doing my own research, I’ve come across the SQLBULKLOAD class. In fact, I’ve found some example code online that shows (at least partially) exactly what I’m trying to do:
using System;
using System.IO;
using System.Collections;
using SQLXMLBULKLOADLib;
using System.Data.OleDb;
using System.Diagnostics;
namespace SQLXmlExample
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string schema = "C:\\ImportSample\\MappingFile.xml";
string datafile = "C:\\ImportSample\\DataFile.xml";
string connectionString = @"provider=SQLOLEDB;data source=localhost;database=SqlXmlDemo;Integrated Security=SSPI;";
for (int i = 0; i < args.Length; i++)
{
switch (args[i].ToLower())
{
case "-schema":
schema = args[i + 1];
break;
case "-datafile":
datafile = args[i + 1];
break;
}
}
if (schema == string.Empty || datafile == string.Empty)
{
Console.WriteLine("Missing Schema or Data File. Format: SqlXmlExample -datafile [filename] -schema [filename]");
return;
}
Load(datafile, schema, connectionString);
}
static public void Load(string XMLFilename, string XMLMappingFilename, string ConnectionString)
{
SQLXMLBULKLOADLib.SQLXMLBulkLoad loader = new SQLXMLBULKLOADLib.SQLXMLBulkLoad();
loader.CheckConstraints = true;
loader.XMLFragment = true;
loader.SchemaGen = true;
loader.SGDropTables = false;
loader.Transaction = false;
loader.ConnectionString = ConnectionString;
loader.Execute("C:\\ImportSample\\MappingFile.xml", "C:\\ImportSample\\DataFile.xml");
}
}
}
With the code above, I’m able to import an .XML file into a SQL Server instance. However, my problem is generating the ‘Response’ .XML file to provide information about the bulk loading operation (i.e. how many records were inserted, if the insert was successful).
As it stands now, I’m thinking that I may be using the wrong class for what I’m trying to accomplish. Am I using the correct class, or should I be using a different one?
If possible, could anyone point me in the direction of some more material to assist me? Any help would be greatly appreciated.
How about using the ErrorLogFile?
This is where the bulk loader stores all its errors and messages, simply try one out, make it fail, see what the format is and then you can then load, parse and generate xml?