wsclient.vb

来自「wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重」· VB 代码 · 共 149 行

VB
149
字号
Option Explicit On
Option Strict On

Imports System.Web.Services.Protocols
Imports System.Data

Public Class WSClient
    Private dsNwind As New DataSet
    Private wsNwind As New DataSetWS.DataSetWS
    Private strTableName As String
    Private blnRowAdded As Boolean

    Private Sub btnLoadCustsDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadCustsDS.Click
        LoadCustomersDataSet()
    End Sub

    Private Sub btnLoadOrdersDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadOrdersDS.Click
        LoadOrdersDataSet()
    End Sub

    Private Sub btnUpdateDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateDS.Click
        UpdateDataSet()
    End Sub

    Private Sub LoadCustomersDataSet()
        dsNwind.Clear()
        Try
            dsNwind = wsNwind.GetAllCustomers
            strTableName = "Customers"
            LoadDataGrid()
        Catch excSoap As SoapException
            MsgBox(excSoap.Message + excSoap.Code.ToString, , "GetAllCustomers SOAP Exception")
        Catch excSys As System.Exception
            MsgBox(excSys.Message + excSys.StackTrace, , "GetAllCustomers System Exception")
        End Try
    End Sub

    Private Sub LoadOrdersDataSet()
        dsNwind.Clear()
        Try
            dsNwind = wsNwind.GetOrdersByCustomerID(mtbCustID.Text)
            strTableName = "Orders"
            LoadDataGrid()
        Catch excSoap As SoapException
            MsgBox(excSoap.Message + excSoap.Code.ToString, , "GetOrdersByCustomerID SOAP Exception")
        Catch excSys As System.Exception
            MsgBox(excSys.Message + excSys.StackTrace, , "GetOrdersByCustomerID System Exception")
        End Try
    End Sub

    Private Sub LoadDataGrid()
        With dgvTable
            .DataSource = dsNwind
            .DataMember = strTableName
            .AutoGenerateColumns = True
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
            .RowsDefaultCellStyle.WrapMode = DataGridViewTriState.False
            .RowHeadersWidth = 30
            .Columns(0).ReadOnly = True
            If .Rows.Count > 1 Then
                btnUpdateDS.Enabled = True
            Else
                btnUpdateDS.Enabled = False
            End If
            blnRowAdded = False
            With dsNwind
                .WriteXml(Application.StartupPath + "\" + strTableName + "DG.xml", XmlWriteMode.DiffGram)
                .WriteXml(Application.StartupPath + "\" + strTableName + ".xml", XmlWriteMode.IgnoreSchema)
                .WriteXmlSchema(Application.StartupPath + "\" + strTableName + ".xsd")
            End With
        End With
    End Sub

    Private Sub UpdateDataSet()
        If dsNwind.HasChanges Then
            Try
                Dim dsChanges As DataSet
                dsChanges = dsNwind.GetChanges
                If strTableName = "Orders" Then
                    If wsNwind.UpdateOrdersDataSet(dsChanges) Then
                        'Updates succeeded
                        dsNwind.AcceptChanges()
                        If blnRowAdded Then
                            'Reload the DataSet to get OrderID value
                            btnLoadOrdersDS_Click(Nothing, Nothing)
                            blnRowAdded = False
                        End If
                    Else
                        'Shoudn't occur
                        MsgBox("Orders update failed without throwing an exception.", , "Update Orders Failure")
                    End If
                Else
                    If wsNwind.UpdateCustomersDataSet(dsChanges) Then
                        'Updates succeeded
                        dsNwind.AcceptChanges()
                    Else
                        'Definitely shoudn't occur
                        MsgBox("Customers update failed without throwing an exception.", , "Update Customers Failure")
                    End If
                End If
            Catch excSoap As SoapException
                MsgBox(excSoap.Message + excSoap.Code.ToString, , "SOAP Exception")
            Catch excSys As Exception
                MsgBox(excSys.Message + excSys.StackTrace, , "System Exception")
            Finally
                dgvTable.Columns(0).ReadOnly = True
            End Try
        Else
            MsgBox("No changes to process.", , "Update DataSetWS")
        End If
    End Sub

    Private Sub dgvTable_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dgvTable.UserAddedRow
        'Allow primary key changes for inserts only
        With dgvTable
            dgvTable.Columns(0).ReadOnly = False
            If strTableName = "Orders" Then
                'Set required values for convenience
                .CurrentCell = .Rows(.CurrentCell.RowIndex).Cells(0)
                .CurrentCell.Value = 1
                .CurrentCell = .Rows(.CurrentCell.RowIndex).Cells(1)
                .CurrentCell.Value = mtbCustID.Text
                .CurrentCell = .Rows(.CurrentCell.RowIndex).Cells(2)
                .CurrentCell.Value = 2
                .CurrentCell = .Rows(.CurrentCell.RowIndex).Cells(3)
                .CurrentCell.Value = Today
                .CurrentCell = .Rows(.CurrentCell.RowIndex).Cells(6)
                .CurrentCell.Value = 3
                .CurrentCell = .Rows(.CurrentCell.RowIndex).Cells(1)
                blnRowAdded = True
            End If
        End With
    End Sub

    Private Sub WSClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mtbCustID.Text = "RATTC"
        'No code example for this property in help
        mtbCustID.ValidatingType = GetType(String)
    End Sub

    Private Sub mtbCustID_MaskInputRejected(ByVal sender As Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles mtbCustID.MaskInputRejected
        btnLoadOrdersDS.Enabled = False
    End Sub

    Private Sub mtbCustID_TypeValidationCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs) Handles mtbCustID.TypeValidationCompleted
        btnLoadOrdersDS.Enabled = True
    End Sub
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?