Click here to Skip to main content
Licence CPOL
First Posted 23 Aug 2007
Views 21,697
Downloads 1,039
Bookmarked 13 times

XML Reader - Import data from XML file to MSSQL table

By | 23 Aug 2007 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DhanuWeedagam

Web Developer

Sri Lanka Sri Lanka

Member

I am Dhanushka Weedagama and I work at Pearson Lanka (pvt) Ltd as a senior software engineer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralXML Reader Pinmemberabdellaoui4:42 13 Aug '08  
Questionneed help in reading this type of xml schema. Pinmember.Net Proffesional1:25 9 Oct '07  
AnswerRe: need help in reading this type of xml schema. Pinmemberwilliam0120:08 30 Oct '09  
QuestionXML File? PinmemberManjit Dosanjh0:07 25 Aug '07  
AnswerRe: XML File? PinmemberDhanuWeedagam17:38 26 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by DhanuWeedagam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid