welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, February 16, 2011

How to insert XML data into SQL Database...


Here i can simply explain to store xml data into sql database ,

create a SQL database like this...
1)carsid           varchar(50)
2)carsmodel     varchar(50)
3)carscolor       varchar(50)

This is a xml file to store into sql table..

- <system>
- <message>
- <message>
- <message>

Then enter the code behind submit button to store xml data into sql databse..

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Carsid, Carsmodel, Carscolor,str As String

 Dim sXMLFilePath As String = "C:\users\admin\desktop\12.xml"

        Dim document As New XmlDocument()

        Dim xmlNodeList As XmlNodeList = document.SelectNodes("system/message")
        For Each node As XmlNode In xmlNodeList
            Carsid = Integer.Parse(node("Carsid").InnerText)
            Carsmodel = node("Carsmodel").InnerText
            Carscolor = node("Carscolor").InnerText

            str = "INSERT INTO car (Carsid,Carsmodel,Carscolor) values ('" & Carsid & "', '" & Carsmodel & "','" & Carscolor & "')"
            sqlcmd = New SqlCommand(str, sqlcon)
                    sqlcmd.CommandType = CommandType.Text

    End Sub

i hope this helpful to u.................


Post a Comment