Asked By
Trevor Jhon
40 points
N/A
Posted on - 10/09/2012
Hello Experts,
How to connect xml to table SQL server VB 6?
I just knew the VB 6 and I am very interested on the coding style and techniques.
But I want to know on how connect VB 6 to a database like SQL server with xml codes.
I need your suggestions and possible solution for this issue.
Thank you.
How to connect xml to table SQL Server vb6?
Following are the methods
If exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[saveXMLresponse]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
   drop procedure [dbo].[saveXMLresponse]
   GO
   Â
   if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Answers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
   drop table [dbo].[Answers]
   GO
   Â
   CREATE TABLE [dbo].[Answers] (
       [EvalID] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
       [Qnum] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
       [Answer] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
   ) ON [PRIMARY]
   GO
   Â
   SET QUOTED_IDENTIFIER ON
   GO
   SET ANSI_NULLS OFF
   GO
   Â
   CREATE PROC saveXMLresponse
   @evaldata varchar(4000)
   AS
   DECLARE @hDoc int
   exec sp_xml_preparedocument @hDoc OUTPUT,@evaldata
   Â
   –truncate table Answers
   INSERT INTO Answers
   SELECT *
   FROM OPENXML (@hDoc,'/insert/Answers')
   WITH Answers
   Â
   Â
   EXEC sp_xml_removedocument @hDoc
   GO
   SET QUOTED_IDENTIFIER OFF
   GO
   SET ANSI_NULLS ON
   GO
TO CREATE THE CONNECTION WITH SQL SERVER
Option Explicit
   Â
   Const ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=MyServer"
   Â
   Private Sub Command1_Click()
   Dim l_conAdoCon As ADODB.Connection
     Â
       Set l_conAdoCon = CreateObject("ADODB.Connection")
     Â
       l_conAdoCon.Open ConnectionString
     Â
       If l_conAdoCon.State <> adStateOpen Then
           MsgBox "Connection could not be established …aborting."
           End
       End If
     Â
   Dim l_strXMLString As String
   Dim i As Integer
   Â
       l_strXMLString = "<insert>" & vbCrLf
     Â
       For i = 1 To 12
           l_strXMLString = l_strXMLString & "<Answers EvalID='" & i & "' Qnum='" & i & "' Answer='" & MonthName(i) & "'/>" & vbCrLf
       Next
     Â
       l_strXMLString = l_strXMLString & "</insert>" & vbCrLf
         Â
       l_conAdoCon.SaveXMLResponse l_strXMLString
     Â
       Set l_conAdoCon = Nothing
   End Sub
Remember to change the connection string in the VB code to your server and database where you created the table and SP.