Net 操作WebServices 的例子
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
Imports System.IO
Imports System.Xml
Imports System.Net
'导入命称空间
Public Class Form1
Public URL4 As String = "http://123.my-portal.cn:8080/sap/bc/soap/rfc?sap-client=123" //Webservices 的地址
Public ok As New ZWRORDER0002.ZWRORDER0002Service
Public cred As NetworkCredential
Public pc(0) As ZWRORDER0002.ZWRTORDER_PCH
'创建数据表
Function CreateTable() As Boolean
' 建立一个新的数据表, 鉴于程序复杂程度问题, 此处没有判断是否已经存在这个数据表
' 如果创建了新数据表,则返回 true。
' 如果未创建新数据表,则返回 false。
Try
'创建数据表
Dim con As SqlCeConnection
con = New SqlCeConnection("DataSource=\mysdf.sdf")
Dim com As SqlCeCommand
com = New SqlCeCommand("create table PC (PCNO NVarChar(10) primary key,XH NVarChar(10) NOT NULL,WERKS NVarChar(4) NOT NULL,PC_DATE NVarChar(10))")
con.Open()
com.Connection = con
com.ExecuteNonQuery()
con.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return False
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建本地数据库,创建本地表
If File.Exists("\mysdf.sdf") Then
MessageBox.Show("Database exists!!")
Else
Dim eng As SqlCeEngine
eng = New SqlCeEngine("DataSource=\mysdf.sdf")
eng.CreateDatabase()
eng.Dispose()
MessageBox.Show("success")
'File.Delete("\mysdf.sdf")
End If
Dim result As Boolean
result = CreateTable()
If result = False Then
MessageBox.Show("Table exist!")
ElseIf result = True Then
MessageBox.Show("Create table success!")
End If
Try
Dim con As SqlCeConnection
con = New SqlCeConnection("DataSource=\mysdf.sdf")
'删除现有数据
Dim com1 As SqlCeCommand
com1 = New SqlCeCommand("DELETE FROM PC")
con.Open()
com1.Connection = con
com1.ExecuteNonQuery()
com1.Dispose()
con.Close()
MessageBox.Show("delete success!!")
'插入数据
'创建数据库
'如果数据库存在,就不建立,如果不存在就建立
'连接WEB服务,获取同步数据
'ok.Url = URL4
''并验证的用户名和密码
cred = New NetworkCredential("123456", "123456")
ok.Credentials = cred
Dim d1 As String
Dim d2 As String
Dim mill As String
d1 = "20060301"
d2 = "20070301"
mill = "7950"
//传递参数
ok.ZWRORDER0002(d1, d2, pc, mill)
Dim i As Integer
For i = 0 To pc.Length - 1
Dim com As SqlCeCommand
com = New SqlCeCommand("insert into PC values ('" + pc(i).PM.ToString + "','" + pc(i).PN.ToString + "','" + pc(i).MILL.ToString + "','" + pc(i).BEGDA.ToString + "')")
con.Open()
com.Connection = con
com.ExecuteNonQuery()
com.Dispose()
con.Close()
Next
MessageBox.Show("success_charu")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
'显示数据集
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
'显示数据集
Dim con As SqlCeConnection
con = New SqlCeConnection("DataSource=\mysdf.sdf")
Dim com As SqlCeCommand
com = New SqlCeCommand("select * from PC")
Dim adapter As SqlCeDataAdapter
adapter = New SqlCeDataAdapter()
adapter.SelectCommand = com
Dim data As DataTable
data = New DataTable
con.Open()
com.Connection = con
adapter.Fill(data)
Dim form As New Form2()
form.SetData(data, 0)
form.ShowDialog()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class