form1.vb
来自「清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码」· VB 代码 · 共 260 行
VB
260 行
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.Common
Namespace Merge_Repl
'' <summary>
'' Summary description for Form1.
'' </summary>
Public Class FormMR
Inherits System.Windows.Forms.Form
Private buttonAddCar As System.Windows.Forms.Button
Private textBoxLocation As System.Windows.Forms.TextBox
Private label2 As System.Windows.Forms.Label
Private textBoxReg As System.Windows.Forms.TextBox
Private label1 As System.Windows.Forms.Label
Private buttonMerge As System.Windows.Forms.Button
Private dataGridCars As System.Windows.Forms.DataGrid
Private mainMenu1 As System.Windows.Forms.MainMenu
Private carsBindingSource As BindingSource
Private components As System.ComponentModel.IContainer
Private dsCars As DataSet
Private daCars As SqlCeDataAdapter
Private cnCars As SqlCeConnection
Public Sub New()
''
'' Required for Windows Form Designer support
''
InitializeComponent()
End Sub
'' <summary>
'' Clean up any resources being used.
'' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
'' <summary>
'' Required method for Designer support - do not modify
'' the contents of me method with the code editor.
'' </summary>
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.mainMenu1 = New System.Windows.Forms.MainMenu()
Me.buttonAddCar = New System.Windows.Forms.Button()
Me.textBoxLocation = New System.Windows.Forms.TextBox()
Me.label2 = New System.Windows.Forms.Label()
Me.textBoxReg = New System.Windows.Forms.TextBox()
Me.label1 = New System.Windows.Forms.Label()
Me.buttonMerge = New System.Windows.Forms.Button()
Me.carsBindingSource = New System.Windows.Forms.BindingSource(Me.components)
Me.dataGridCars = New System.Windows.Forms.DataGrid()
Me.SuspendLayout()
''
'' buttonAddCar
''
Me.buttonAddCar.Location = New System.Drawing.Point(64, 184)
Me.buttonAddCar.Name = "buttonAddCar"
Me.buttonAddCar.Size = New System.Drawing.Size(64, 20)
Me.buttonAddCar.TabIndex = 2
Me.buttonAddCar.Text = "Add"
AddHandler Me.buttonAddCar.Click, AddressOf Me.buttonAddCar_Click
''
'' textBoxLocation
''
Me.textBoxLocation.Location = New System.Drawing.Point(64, 152)
Me.textBoxLocation.Name = "textBoxLocation"
Me.textBoxLocation.Size = New System.Drawing.Size(160, 21)
Me.textBoxLocation.TabIndex = 3
''
'' label2
''
Me.label2.Location = New System.Drawing.Point(8, 160)
Me.label2.Name = "label2"
Me.label2.Size = New System.Drawing.Size(64, 12)
Me.label2.Text = "Location:"
''
'' textBoxReg
''
Me.textBoxReg.Location = New System.Drawing.Point(64, 128)
Me.textBoxReg.Name = "textBoxReg"
Me.textBoxReg.Size = New System.Drawing.Size(64, 21)
Me.textBoxReg.TabIndex = 5
''
'' label1
''
Me.label1.Location = New System.Drawing.Point(8, 136)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(32, 16)
Me.label1.Text = "Reg:"
''
'' buttonMerge
''
Me.buttonMerge.Location = New System.Drawing.Point(64, 224)
Me.buttonMerge.Name = "buttonMerge"
Me.buttonMerge.Size = New System.Drawing.Size(88, 24)
Me.buttonMerge.TabIndex = 1
Me.buttonMerge.Text = "Merge"
AddHandler Me.buttonMerge.Click, AddressOf Me.buttonMerge_Click
''
'' dataGridCars
''
Me.dataGridCars.DataSource = Me.carsBindingSource
Me.dataGridCars.Location = New System.Drawing.Point(8, 8)
Me.dataGridCars.Name = "dataGridCars"
Me.dataGridCars.Size = New System.Drawing.Size(224, 112)
Me.dataGridCars.TabIndex = 0
''
'' FormMR
''
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
Me.ClientSize = New System.Drawing.Size(240, 268)
Me.Controls.Add(Me.dataGridCars)
Me.Controls.Add(Me.buttonMerge)
Me.Controls.Add(Me.buttonAddCar)
Me.Controls.Add(Me.textBoxLocation)
Me.Controls.Add(Me.label2)
Me.Controls.Add(Me.textBoxReg)
Me.Controls.Add(Me.label1)
Me.Menu = Me.mainMenu1
Me.MinimizeBox = False
Me.Name = "FormMR"
Me.Text = "Traffic"
AddHandler Me.Closing, AddressOf Me.FormMR_Closing
AddHandler Me.Load, AddressOf Me.FormMR_Load
Me.ResumeLayout(False)
End Sub
#End Region
'' <summary>
'' The main entry point for the application.
'' </summary>
Shared Sub Main()
Application.Run(New FormMR())
End Sub
Private Sub FormMR_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'' Configure the DataAdapter for use in UI binding
cnCars = New SqlCeConnection("Data Source=\My Documents\TrafficMR.sdf")
daCars = New SqlCeDataAdapter("SELECT CarId, Reg, Location FROM Cars", cnCars)
'' Configure the update comand
Dim cb As New SqlCeCommandBuilder(daCars)
Merge()
End Sub
Private Sub Merge()
Const URL As String = "http://homelaptop/mergerep/sqlcesa30.dll" '' URL to Agent
Const InterNetUser As String = "homelaptop\danial" '' InternetUser
Const InternetPassword As String = "danial" '' InternetPassword
Const PublisherServer As String = "homelaptop" '' Publisher server
Const PublisherDatabase As String = "Traffic" '' Publisher Database
Const PublicationName As String = "TrafficMR" '' Publication name
Const SubscriberName As String = "Testing" '' Subscriber name
Const LocalDB As String = "Data Source=\My Documents\TrafficMR.sdf" ''Connection string to local database
Using rep As New SqlCeReplication(URL, InterNetUser, InternetPassword, PublisherServer, PublisherDatabase, PublicationName, SubscriberName, LocalDB)
Try
If Not System.IO.File.Exists("\My Documents\TrafficMR.sdf") Then
rep.AddSubscription(AddOption.CreateDatabase)
End If
rep.Synchronize()
Catch ex As SqlCeException
DisplaySQLCEErrors(ex)
End Try
End Using
SetupDataSetforUI()
End Sub
Private Sub SetupDataSetforUI()
'' Set up the dataset which is the data source for the UI
If dsCars Is Nothing Then
dsCars = New DataSet()
End If
Try
dsCars.Clear()
daCars.Fill(dsCars, "Cars")
dsCars.Tables("Cars").Columns("CarID").AutoIncrement = True
''Get current highest value in that column
cnCars.Open()
Dim MaxCarID As Integer = -1
Using cmd As New SqlCeCommand("SELECT MAX(CarID) FROM Cars", cnCars)
Dim result As Object = cmd.ExecuteScalar()
If Not IsDBNull(result) Then
MaxCarID = Convert.ToInt32(result)
End If
End Using
cnCars.Close()
'' Set the AutoIncrement seed accordingly
Me.dsCars.Tables("Cars").Columns("CarID").AutoIncrementSeed = MaxCarID + 1
carsBindingSource.DataSource = dsCars
carsBindingSource.DataMember = "Cars"
Catch ex As SqlCeException
DisplaySQLCEErrors(ex)
End Try
End Sub
Private Sub DisplaySQLCEErrors(ByVal ex As SqlCeException)
For i As Integer = 0 To ex.Errors.Count - 1
MessageBox.Show("Index #" + i.ToString() + vbNewLine + ex.Errors(i).Source + vbNewLine + "Error: " + ex.Errors(i).Message, "Error No. " + ex.Errors(i).NativeError.ToString())
Next
End Sub
Private Sub buttonAddCar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.dsCars.Tables("Cars").Rows.Add(New Object() {Nothing, Me.textBoxReg.Text, Me.textBoxLocation.Text})
Try
'' Call Update on the DataAdapter to write to database
daCars.Update(dsCars, "Cars")
Catch ex As SqlCeException
DisplaySQLCEErrors(ex)
End Try
End Sub
Private Sub buttonMerge_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Merge()
MessageBox.Show("Merge complete")
End Sub
Private Sub FormMR_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
cnCars.Close()
cnCars.Dispose()
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?