Question

Ok. So I am pretty new a this. I have a datatable that I want to pass to a stored procedure for further manipulation. I have read some stuff on the web and it looks like I should be able to convert the datatable to XML and then pass that to the stored procedure. What am I doning wrong? I have SQL server 2005. The data never gets passed to the stored procedure.

Sub Test()
    Dim dt As New DataTable

    Fill datatable code omitted. there are 150 rows in the datatable after this

    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim x As XmlDocument
    x.LoadXml(dt.DataSet.GetXml)
    Dim ta As New dsTestTableAdapters.TESTRxTableAdapter
    ta.ProcessFile(x)
End Sub

The Stored procedure looks like this...

ALTER PROCEDURE [dbo].[ProcessFile]

    (
         @x XML

    )
AS
BEGIN

'DO STUFF HERE

END
Was it helpful?

Solution 2

OK. This is what I go to work. I don't know if this is the best way, but this works for me.

Sub Test
    Dim ds as new DataSet ("Testds")
    Dim dt as New DataTable("Testdt")
    'Fill Datatable code omitted

Dim ta as new dsTest.TestTableAdapter
    'TableAdapter returns a datatable to ensure that the stored procedure worked
    ta.AddDataToDB(dt,ds.GetXML)

    Me.DataGridView1.DataSource=dt
    End Sub

The Stored Procedure Looks like this.

CREATE PROCEDURE [dbo].[AddDataToDB]

    (
         @x XML

    )
AS
    BEGIN
        DECLARE @data TABLE (
                            TestCol1 VARCHAR(50),
                            [TestCol2] VARCHAR(50), 
                            [TestCol3] VARCHAR(50), 
                            )

        INSERT INTO @data (
                           [TestCol1],
                           [TestCol2],
                           [TestCol3]
                          )
        SELECT 
            xmlVals.rowvals.query('TestCol1').value('.','VARCHAR(50)'),
            xmlVals.rowvals.query('TestCol2').value('.','VARCHAR(50)'),
            xmlVals.rowvals.query('TestCol3').value('.','VARCHAR(50)')
        FROM 
            @x.nodes('/Testds/Testdt') as xmlVals(rowvals)



        SELECT * FROM @data     

    END

OTHER TIPS

In the past I have done something similar but with SQL 2000. In SQL 2000 there was no XML data type so I had to receive the output from DataSet.GetXML via a ntext parameter into the stored procedure then processed it using sp_xml_preparedocument and sp_xml_removedocument, so the process should work for you.

It may be that you do not need to load an XmlDocument with the text and can just pass the xml text as is to the stored procedure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top