Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XML Reader - Import data from XML file to MSSQL table

0.00/5 (No votes)
23 Aug 2007 3  
Import data from a bulky XML file to a MSSQL table.

Introduction

This document describes a simple way of importing data from XML file to a MSSQL table. People involving in data coversion activities can easily use this application to convert their bulky MXL inputs.

Screenshot - Reader.jpg

Overview

The sample project can load an XML file and read it and show it in a data grid view. Then you can write dataset to a MSSQL table. In this article I am describing the way of writing the dataset to a MSSQL table. But you can write the dataset to any type of Databse table.

Using the code

The sample application uses ReadXml method of dataset to read a XML file.

Then you can save these data to a MSSQL data table.

//

// 

SqlConnection myconnection = new SqlConnection();

SqlDataAdapter myda=new SqlDataAdapter();

myconnection.ConnectionString = "Data Source=DHANUSHKA\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";

string strsql = "INSERT INTO Data(name, age)VALUES(@name,@age)";

myconnection.Open();

SqlCommand myinsertcommand = new SqlCommand(strsql, myconnection);

myinsertcommand.Parameters.Add("@name", SqlDbType.VarChar, 15, "name");

myinsertcommand.Parameters.Add("@age", SqlDbType.VarChar, 15, "age");

myda.InsertCommand = myinsertcommand;

myda.Update(myds.Tables["Data"]);

//

When adding parameters to the insert command of data adapter @name and @age are tag names in XML file and "name" and "age" are column names in MSSQL data table.

Points of Interest

One of my friends asked me that he wants to import data from a bulky XML file to a MSSQL table. After few minutes of effort I came up with a nice simple application having few lines of code. And also my friend next to my block, Chamin, helped me on this.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here