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 并将新数据插入适当的表格。

  1<webmethod()> _ 
  2
  3Public Function InsertFeedback(ByVal ds As DataSet, ByVal username As 
  4
  5String) As Boolean 
  6
  7Dim con As New SqlConnection(connectionstring) 
  8
  9Dim cmd As New SqlCommand("p_Feedback_i", con) 
 10
 11cmd.CommandType = CommandType.StoredProcedure 
 12
 13'  设置参数 
 14
 15Dim prmXML As SqlParameter = cmd.Parameters.Add("@XML", 
 16
 17SqlDbType.NText) 
 18
 19prmXML.Direction = ParameterDirection.Input 
 20
 21prmXML.Value = ds.GetXml 
 22
 23Dim prmUsername As SqlParameter = cmd.Parameters.Add("@Username", 
 24
 25SqlDbType.NVarChar) 
 26
 27prmUsername.Direction = ParameterDirection.Input 
 28
 29prmUsername.Value = username 
 30
 31Try 
 32
 33con.Open() 
 34
 35cmd.ExecuteNonQuery() 
 36
 37Catch ex As Exception 
 38
 39'  处理、记录并重掷错误 
 40
 41Throw ex 
 42
 43Finally 
 44
 45con.Close() 
 46
 47End Try 
 48
 49Return True 
 50
 51End Function 
 52
 53The stored procedure inserts the new data: 
 54
 55CREATE PROCEDURE p_Feedback_i 
 56
 57@XML ntext, 
 58
 59@Username nvarchar(50) 
 60
 61AS 
 62
 63SET NOCOUNT ON 
 64
 65DECLARE @iDoc  integer 
 66
 67DECLARE @Error  integer 
 68
 69/* Create XML document. */ 
 70
 71EXEC sp_xml_preparedocument @iDoc OUTPUT, @XML 
 72
 73/* Insert new records */ 
 74
 75INSERT INTO  Feedback 
 76
 77( 
 78
 79FeedbackID, 
 80
 81PlantSection, 
 82
 83Part, 
 84
 85DefectScope, 
 86
 87ScopeID, 
 88
 89DefectType, 
 90
 91RichInk, 
 92
 93Username 
 94
 95) 
 96
 97SELECT  ID, 
 98
 99PlantSection, 
100
101Part, 
102
103DefectScope, 
104
105ScopeID, 
106
107DefectType, 
108
109RichInk, 
110
111@Username 
112
113FROM OPENXML (@iDoc, '/DataSet/Feedback',2) WITH 
114
115( 
116
117ID  uniqueidentifier, 
118
119PlantSection  int, 
120
121Part  int, 
122
123DefectScope  int, 
124
125ScopeID  nvarchar(50), 
126
127DefectType  int, 
128
129RichInk  nvarchar(50) 
130
131) 
132
133SELECT @Error = @@ERROR 
134
135IF (@Error &lt;&gt; 0) 
136
137BEGIN 
138
139GOTO Errorhandler 
140
141END 
142
143/* Remove the XML document*/ 
144
145EXEC sp_xml_removedocument @iDoc 
146
147RETURN 
148
149Errorhandler: 
150
151IF NOT @iDoc IS NULL 
152
153EXEC sp_xml_removedocument @iDoc 
154
155RAISERROR (@Error,16,1) 
156
157RETURN</webmethod()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus