Results 1 to 1 of 1

Thread: VB - Passing XML string to Bulk Update data in SQL server.

  1. #1

    Thread Starter
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

    VB - Passing XML string to Bulk Update data in SQL server.

    This is the script for a table and a stored procedure .. create them as it is in some database on your sql server

    VB Code:
    1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[saveXMLresponse]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    2. drop procedure [dbo].[saveXMLresponse]
    3. GO
    4.  
    5. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Answers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    6. drop table [dbo].[Answers]
    7. GO
    8.  
    9. CREATE TABLE [dbo].[Answers] (
    10.     [EvalID] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    11.     [Qnum] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    12.     [Answer] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
    13. ) ON [PRIMARY]
    14. GO
    15.  
    16. SET QUOTED_IDENTIFIER ON
    17. GO
    18. SET ANSI_NULLS OFF
    19. GO
    20.  
    21. CREATE PROC saveXMLresponse
    22. @evaldata varchar(4000)
    23. AS
    24. DECLARE @hDoc int
    25. exec sp_xml_preparedocument @hDoc OUTPUT,@evaldata
    26.  
    27. --truncate table Answers
    28. INSERT INTO Answers
    29. SELECT *
    30. FROM OPENXML (@hDoc,'/insert/Answers')
    31. WITH Answers
    32.  
    33.  
    34. EXEC sp_xml_removedocument @hDoc
    35. GO
    36. SET QUOTED_IDENTIFIER OFF
    37. GO
    38. SET ANSI_NULLS ON
    39. GO

    VB Code:
    1. Option Explicit
    2.  
    3. Const ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=MyServer"
    4.  
    5. Private Sub Command1_Click()
    6. Dim l_conAdoCon As ADODB.Connection
    7.    
    8.     Set l_conAdoCon = CreateObject("ADODB.Connection")
    9.    
    10.     l_conAdoCon.Open ConnectionString
    11.    
    12.     If l_conAdoCon.State <> adStateOpen Then
    13.         MsgBox "Connection could not be established ...aborting."
    14.         End
    15.     End If
    16.    
    17. Dim l_strXMLString As String
    18. Dim i As Integer
    19.  
    20.     l_strXMLString = "<insert>" & vbCrLf
    21.    
    22.     For i = 1 To 12
    23.         l_strXMLString = l_strXMLString & "<Answers EvalID='" & i & "' Qnum='" & i & "' Answer='" & MonthName(i) & "'/>" & vbCrLf
    24.     Next
    25.    
    26.     l_strXMLString = l_strXMLString & "</insert>" & vbCrLf
    27.        
    28.     l_conAdoCon.SaveXMLResponse l_strXMLString
    29.    
    30.     Set l_conAdoCon = Nothing
    31. End Sub

    and use this code to add 12 records to the table using XML and ADO. This is just a small sample of what you can do with XML and SQL.

    Remeber to change the connectionstring in the VB code to your server and database where you created the table and SP

    Enjoy ....
    Last edited by techyspecy; Jun 27th, 2003 at 12:21 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width