Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML import to SQL Server 2005

Posted on 2008-10-22
3
Medium Priority
?
431 Views
Last Modified: 2012-06-21
I am tyring to create either a stored procedure in SQL Server 2005 or a VB application that I can call through command line that will import an xml file into SQL Server 2005.  I have hundreds of xml files to import and I was also provided with the Schema in an xsd file.  I have been able to generate the database tables based on the schema, but nothing I have tried works for importing the data.  Does anyone have working sample code for the Import?  Thank You.
0
Comment
Question by:epitches
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:Sander Stad
ID: 22783532
Here is an article that can help you. It is a tutorial how to bulk load xml documents into a database.
http://technet.microsoft.com/en-us/library/ms171806.aspx 
0
 
LVL 11

Accepted Solution

by:
Muhammad Kashif earned 2000 total points
ID: 22784044
Following code will read From XML file and write to sql server.
    Public Sub Read_XML_File()
 
        Dim ds As New DataSet
 
        'Read File to Data Set
        ds.ReadXml("FileName.xml")
 
        Dim TableCounter As Integer = 0
        Dim dr As DataRow
        Dim dataTableXml As Data.DataTable
        Dim sqlCmd As String = ""
        Dim ColCounter As Integer = 0
 
        For TableCounter = 0 To ds.Tables.Count - 1
            dataTableXml = ds.Tables(TableCounter)
            For Each dr In dataTableXml.Rows
                sqlCmd = ""
                sqlCmd = "insert into [" & dataTableXml.TableName & "] ("
 
                For i As Integer = 0 To dataTableXml.Columns.Count - 1
                    sqlCmd = sqlCmd + dataTableXml.Columns(i).ColumnName.ToString() + ","
                Next
                sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ") values ("
 
                For x As Integer = 0 To dataTableXml.Columns.Count - 1
 
 
                    Dim d As System.DateTime
                    Dim isDate As Boolean
                    If (DateTime.TryParse(dr(x).ToString(), d)) Then    'Checks If the Column is of Type Date
                        isDate = True
                    Else
                        isDate = False
                    End If
                    If (isDate) Then
                        sqlCmd = sqlCmd + "'" + Format(CDate(d.ToString), "dd/MMM/yyyy") + "',"
                    ElseIf InStr(dr(x).ToString, "'", CompareMethod.Text) <> 0 Then
                        sqlCmd = sqlCmd + "'" + dr(x).ToString().Replace("'", "''") + "',"
                    Else
                        sqlCmd = sqlCmd + "'" + dr(x) + "',"
                    End If
 
                Next
                sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");"
                'Now Insert Query is prepared
                'Run This query; Record will be inserted
            Next
        Next
 
 
    End Sub

Open in new window

0
 

Author Closing Comment

by:epitches
ID: 31509091
Thank you!! This worked great.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question