vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, February 16, 2011

How to insert XML data into SQL Database...


hi..

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>
  <Carsid>1</Carsid>
  <Carsmodel>Toyota</Carsmodel>
  <Carscolor>Green</Carscolor>
  </message>
- <message>
  <Carsid>2</Carsid>
  <Carsmodel>Kia</Carsmodel>
  <Carscolor>Yellow</Carscolor>
  </message>
- <message>
  <Carsid>3</Carsid>
  <Carsmodel>swift</Carsmodel>
  <Carscolor>silver</Carscolor>
  </message>
  </system>

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()
        document.Load(sXMLFilePath)




        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
                    sqlcmd.ExecuteNonQuery()
        Next




    End Sub

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


0 comments:

Post a Comment