Link to home
Start Free TrialLog in
Avatar of Monaglit
Monaglit

asked on

uploading xml to database vb.net

URGENT !!

Can someone give me the vb.net code to upload xml file to sql server. very urgent.thanks.
Avatar of kaufmed
kaufmed
Flag of United States of America image

One example:
Dim xmlData As XmlDocument ' Assume this is already initialized

Using con As New System.Data.SqlClient.SqlConnection(your_connection_string)
    Using cmd As New System.Data.SqlClient("INSERT INTO [your_table_name] VALUES (@data)", con)
        cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@data", xmlData.OuterXml))

        con.Open()
        cmd.ExecuteNonQuery()
    End Using
End Using

Open in new window

Avatar of Monaglit
Monaglit

ASKER

I am selecting the XML file from hard disk. in that case what will xmldata be?
when i run this i get column name or number of supplied values does not match
i need to put the element values as the column values in the database
I am selecting the XML file from hard disk. in that case what will xmldata be?
You can use the XmlDocument.Load method to read a file from disk:

Dim xmlData As New XmlDocument()

xmlData.Load("C:\path\to\file.xml")

Open in new window


i need to put the element values as the column values in the database

Open in new window

Oh. I got the impression from your question that you were inserting the actual XML into the database. If you need to insert the node values into specific columns, then we would need to see the XML structure and the database column names.
here is the sample xml and table columns

<Records filename="C:\Documents and Settings\Data.csv">
  <Record RowID="1">
    <EmployeeID>D091</EmployeeID>
    <Name>hhh</Name>
    <surname>sss</surname>
    <DateofBirth>2004-4-12 00:00</DateofBirth>
 </Record>
  <Record RowID="2">
    <EmployeeID>C002</EmployeeID>
    <Name>sjjj</Name>
    <surname>kkkk</surname>
    <DateofBirth>
    </DateofBirth>    
  </Record>
</Records>

TableName -  Employee
columns -
Employee_ID varchar(20)
Name varchar(20)
Surname varchar(50)
DateofBirth datetime
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. Already managed to do. But will accept this as the solution.
good upload script.