views:

3329

answers:

4

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
+1  A: 

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.

Jim Birchall
A: 

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
Kalel
A: 

Try using the stored procedure mentioned here: http://whereclause.com/blog/?p=8... Let me know if it helped.

A: 

I is showing nothing in my query analyzer of SQL 2005. can u plz. tell me why it is not working.. at SQL Server 2005 Why..is there any error? I have wrote following code :

DECLARE @productIds xml SET @productIds =' 3 Sun 6 Moon 15 God '

DECLARE @Products TABLE (ID varchar(20),name varchar(20)) INSERT INTO @Products (ID,name) SELECT xmlVals.rowvals.query('id').value('.','VARCHAR(20)'), xmlVals.rowvals.query('name').value('.','VARCHAR(20)')
from @productIds.nodes('/products/rec') as xmlVals(rowvals)

SElect * from @Products