Click here to Skip to main content
14,333,655 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am trying to add the XML file details to SQL Server, I did few coding, getting error, tried to solve it but unsuccessful. This is the code which i used. Can u suggest me where should i change


Dim xmlPath As New String("C:\USERS\Administrator\DESKTOP\xml FILES\books.xml")
Dim xmlQryAs String
xmlQry= "DECLARE @xml XML" + Environment.NewLine() + _
       "SELECT @xml =" + _
       "FROM OPENROWSET( BULK '" + xmlPath + "', SINGLE_BLOB ) " + Environment.NewLine() + _
       "Insert into bkdetails " + Environment.NewLine() + _
       "SELECT * " + Environment.NewLine() + _
       "FROM " + Environment.NewLine() + _
       "(SELECT TITLE,PRICE,GENRE FROM @xml.nodes(books.xml)" + Environment.NewLine()

            Dim conn As New SqlConnection("Data Source=.\SQLExpress;Initial Catalog=Book_Details;Integrated Security=SSPI")
            Dim cmd As New SqlCommand
            With cmd
                .Connection = conn
                .CommandType = CommandType.Text
                .CommandText = (AOSQL)
            End With
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


Incorrect syntax near the keyword 'FROM'.
The table (and its columns) returned by a table-valued method need to be aliased.
Posted
Updated 13-Jul-11 22:29pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try
xmlQry= "DECLARE @xml XML" + Environment.NewLine() + _
       "SELECT @xml =" + _
       "FROM OPENROWSET( BULK '" + xmlPath + "', SINGLE_BLOB ) " + Environment.NewLine() + _
       "Insert into bkdetails " + Environment.NewLine() + _
       "SELECT * " + Environment.NewLine() + _
       "FROM " + Environment.NewLine() + _
       "(SELECT TITLE,PRICE,GENRE FROM @xml.nodes(books.xml)) AS XMLBOOKS" + Environment.NewLine()
   
Comments
amitk_189 14-Jul-11 5:46am
   
Hello Prerak Patel, this worked as i had mistake of ommiting as XMLBOOKS, now its working.
Thanks, thanks a lot
Prerak Patel 14-Jul-11 5:47am
   
You are welcome. Mark it as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100