XML Web Service 数据交换

XML Web Service 数据交换

 

客户端调用服务器端的 Web 服务并传递包含数据的 DataSet (ds)

 

Private Sub Synchronize()

    Dim username As String = "JohnS"

    Dim blnSuccess As Boolean

    ' 使用 XML Web Service 进行同步

    Cursor.Current = Cursors.WaitCursor

    Dim wsFeedback As New wsFeedback.feedback

    blnSuccess = wsFeedback.InsertFeedback(ds, username)

 

    Cursor.Current = Cursors.Default

End Sub

 

服务器端的 Web 服务接受 DataSet 并将 XML 直接传递到 SQL Server 2000 存储的过程中,该过程使用 SQLXML(英文)和 OPENXML 解析 XML 并将新数据插入适当的表格。

 

<WebMethod()> _

Public Function InsertFeedback(ByVal ds As DataSet, ByVal username As

  String) As Boolean

    Dim con As New SqlConnection(connectionstring)

    Dim cmd As New SqlCommand("p_Feedback_i", con)

    cmd.CommandType = CommandType.StoredProcedure

 

    ' 设置参数

    Dim prmXML As SqlParameter = cmd.Parameters.Add("@XML",

      SqlDbType.NText)

    prmXML.Direction = ParameterDirection.Input

    prmXML.Value = ds.GetXml

 

    Dim prmUsername As SqlParameter = cmd.Parameters.Add("@Username",

      SqlDbType.NVarChar)

    prmUsername.Direction = ParameterDirection.Input

    prmUsername.Value = username

 

    Try

    con.Open()

        cmd.ExecuteNonQuery()

    Catch ex As Exception

        ' 处理、记录并重掷错误

        Throw ex

 

    Finally

        con.Close()

    End Try

 

    Return True

 

End Function

 

The stored procedure inserts the new data:

 

CREATE PROCEDURE p_Feedback_i

   @XML ntext,

   @Username nvarchar(50)

 

AS

 

SET NOCOUNT ON

 

DECLARE @iDoc   integer

DECLARE @Error   integer

 

/* Create XML document. */

EXEC sp_xml_preparedocument @iDoc OUTPUT, @XML

 

/* Insert new records */

INSERT INTO    Feedback

      (

      FeedbackID,

      PlantSection,

      Part,

      DefectScope,

      ScopeID,

      DefectType,

      RichInk,

      Username

      )

SELECT     ID,

      PlantSection,

      Part,

      DefectScope,

      ScopeID,

      DefectType,

      RichInk,

      @Username

FROM OPENXML (@iDoc, '/DataSet/Feedback',2) WITH

      (

      ID   uniqueidentifier,

      PlantSection   int,

      Part      int,

      DefectScope   int,

      ScopeID   nvarchar(50),

      DefectType   int,

      RichInk      nvarchar(50)

      )

 

SELECT @Error = @@ERROR

IF (@Error <> 0)

BEGIN

   GOTO Errorhandler

END

 

/* Remove the XML document*/

EXEC sp_xml_removedocument @iDoc

 

RETURN

 

Errorhandler:

 

IF NOT @iDoc IS NULL

   EXEC sp_xml_removedocument @iDoc

 

RAISERROR (@Error,16,1)

 

RETURN

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值