Author:水如烟
总目录:行政区划数据方案设计
上一篇,行政区划程序的设计(十二)
本篇为项目测试。文最后提供当前方案代码下载。
在测试更新数据功能前,先在LzmTW项目上建一个登录SQLServer服务器的登录信息类,
LoginInformations.vb
Namespace
uSystem.uData.uSql
< Serializable() > _
Public Class LoginInformations
Private gServerName As String = " ./SQLExpress "
Private gDatabase As String = " Master "
Private gUserName As String
Private gPassword As String
Private gUseSSPI As Boolean = True
< NonSerialized() > Private gSqlVersion As String = Nothing
< NonSerialized() > Private SQL_CONNECTIONSTRING As String = " Data Source={0};Initial Catalog={1}; "
< NonSerialized() > Private SSPI_STRING As String = " Integrated Security=True; "
< NonSerialized() > Private NOT_SSPI_STRING As String = " Persist Security Info=True;User ID={0};Password={1}; "
Public Property ServerName() As String
Get
Return gServerName
End Get
Set ( ByVal value As String )
gServerName = value
End Set
End Property
Public Property Database() As String
Get
Return gDatabase
End Get
Set ( ByVal value As String )
gDatabase = value
End Set
End Property
Public Property UserName() As String
Get
Return gUserName
End Get
Set ( ByVal value As String )
gUserName = value
End Set
End Property
Public Property Password() As String
Get
Return gPassword
End Get
Set ( ByVal value As String )
gPassword = value
End Set
End Property
Public Property UseSSPI() As Boolean
Get
Return gUseSSPI
End Get
Set ( ByVal value As Boolean )
gUseSSPI = value
End Set
End Property
Public Function TestConnect() As Boolean
Dim mResult As Boolean = False
Dim mConnection As New SqlClient.SqlConnection( Me .ConnectionStringBuilder.ConnectionString)
Try
mConnection.Open()
Me .gSqlVersion = Me .GetSqlVersion(mConnection)
mResult = True
mConnection.Close()
Catch ex As Exception
End Try
mConnection.Dispose()
Return mResult
End Function
Public ReadOnly Property ConnectionStringBuilder() As SqlClient.SqlConnectionStringBuilder
Get
Return New SqlClient.SqlConnectionStringBuilder(GetConnnectionString)
End Get
End Property
Public ReadOnly Property SqlVersion() As String
Get
Return gSqlVersion
End Get
End Property
Private Function GetConnnectionString() As String
Dim mConnectionString As String
mConnectionString = String .Format( Me .SQL_CONNECTIONSTRING, Me .ServerName, Me .Database)
If Me .UseSSPI Then
mConnectionString = String .Concat(mConnectionString, Me .SSPI_STRING)
Else
mConnectionString = String .Concat(mConnectionString, String .Format( Me .NOT_SSPI_STRING, Me .UserName, Me .Password))
End If
Return mConnectionString
End Function
' 取当前SQLServer版本
Private Function GetSqlVersion( ByVal connection As SqlClient.SqlConnection) As String
Dim mResult As String = Nothing
Dim mCommand As New SqlClient.SqlCommand( " Master..xp_msver " , connection)
Dim mDataReader As SqlClient.SqlDataReader
With mCommand
.CommandType = CommandType.StoredProcedure
mDataReader = .ExecuteReader
With mDataReader
While .Read()
If .Item( " Name " ).ToString = " ProductVersion " Then
mResult = .Item( " Character_Value " ).ToString
Exit While
End If
End While
.Close()
End With
.Dispose()
End With
Return mResult
End Function
End Class
End Namespace
< Serializable() > _
Public Class LoginInformations
Private gServerName As String = " ./SQLExpress "
Private gDatabase As String = " Master "
Private gUserName As String
Private gPassword As String
Private gUseSSPI As Boolean = True
< NonSerialized() > Private gSqlVersion As String = Nothing
< NonSerialized() > Private SQL_CONNECTIONSTRING As String = " Data Source={0};Initial Catalog={1}; "
< NonSerialized() > Private SSPI_STRING As String = " Integrated Security=True; "
< NonSerialized() > Private NOT_SSPI_STRING As String = " Persist Security Info=True;User ID={0};Password={1}; "
Public Property ServerName() As String
Get
Return gServerName
End Get
Set ( ByVal value As String )
gServerName = value
End Set
End Property
Public Property Database() As String
Get
Return gDatabase
End Get
Set ( ByVal value As String )
gDatabase = value
End Set
End Property
Public Property UserName() As String
Get
Return gUserName
End Get
Set ( ByVal value As String )
gUserName = value
End Set
End Property
Public Property Password() As String
Get
Return gPassword
End Get
Set ( ByVal value As String )
gPassword = value
End Set
End Property
Public Property UseSSPI() As Boolean
Get
Return gUseSSPI
End Get
Set ( ByVal value As Boolean )
gUseSSPI = value
End Set
End Property
Public Function TestConnect() As Boolean
Dim mResult As Boolean = False
Dim mConnection As New SqlClient.SqlConnection( Me .ConnectionStringBuilder.ConnectionString)
Try
mConnection.Open()
Me .gSqlVersion = Me .GetSqlVersion(mConnection)
mResult = True
mConnection.Close()
Catch ex As Exception
End Try
mConnection.Dispose()
Return mResult
End Function
Public ReadOnly Property ConnectionStringBuilder() As SqlClient.SqlConnectionStringBuilder
Get
Return New SqlClient.SqlConnectionStringBuilder(GetConnnectionString)
End Get
End Property
Public ReadOnly Property SqlVersion() As String
Get
Return gSqlVersion
End Get
End Property
Private Function GetConnnectionString() As String
Dim mConnectionString As String
mConnectionString = String .Format( Me .SQL_CONNECTIONSTRING, Me .ServerName, Me .Database)
If Me .UseSSPI Then
mConnectionString = String .Concat(mConnectionString, Me .SSPI_STRING)
Else
mConnectionString = String .Concat(mConnectionString, String .Format( Me .NOT_SSPI_STRING, Me .UserName, Me .Password))
End If
Return mConnectionString
End Function
' 取当前SQLServer版本
Private Function GetSqlVersion( ByVal connection As SqlClient.SqlConnection) As String
Dim mResult As String = Nothing
Dim mCommand As New SqlClient.SqlCommand( " Master..xp_msver " , connection)
Dim mDataReader As SqlClient.SqlDataReader
With mCommand
.CommandType = CommandType.StoredProcedure
mDataReader = .ExecuteReader
With mDataReader
While .Read()
If .Item( " Name " ).ToString = " ProductVersion " Then
mResult = .Item( " Character_Value " ).ToString
Exit While
End If
End While
.Close()
End With
.Dispose()
End With
Return mResult
End Function
End Class
End Namespace
现在可以写测试代码了,一个窗体三个文件,不包括自动生成的:
FormTestUpdateDatabase.vb
Public
Class
FormTestUpdateDatabase
Private WithEvents gUpdateDatabaseService As New RegionalCodeCenter.UpdateDatabaseService
Private WithEvents gServerServices As RegionalCodeCenter.Services.ServerServices
Private gLogonInformations As New LzmTW.uSystem.uData.uSql.LoginInformations
Private gRegionalCodeDatasFile As String = AppDomain.CurrentDomain.SetupInformation.ApplicationBase & " RegionalCodeDatas.xml "
Private Sub FormTestUpdateDatabase_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
gLogonInformations.Database = " RegionalCodeWorks "
gServerServices = New RegionalCodeCenter.Services.ServerServices(gLogonInformations.ConnectionStringBuilder.ConnectionString)
End Sub
Private Sub gUpdateDatabaseService_ServiceMessage( ByVal sender As Object , ByVal message As String ) Handles gUpdateDatabaseService.ServiceMessage, gServerServices.ServiceMessage
Me .Label1.Text = message
Windows.Forms.Application.DoEvents()
End Sub
Private Sub ButtonCloseExcel_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCloseExcel.Click
gUpdateDatabaseService.ExcelClose()
End Sub
Private Sub gServerServices_UpdateDatabaseCurrentPercent( ByVal sender As Object , ByVal currentPercent As Integer ) Handles gServerServices.UpdateDatabaseCurrentPercent
Me .ProgressBar1.Value = currentPercent
Windows.Forms.Application.DoEvents()
End Sub
Private Sub CheckedListBoxChooseVersionDatesToUpdate_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBoxChooseVersionDatesToUpdate.SelectedIndexChanged
Me .DataGridView1.DataSource = gUpdateDatabaseService.NetRegionalCodeTableDictionary( Me .CheckedListBoxChooseVersionDatesToUpdate.SelectedItem.ToString)
Me .Label2.Text = String .Format( " 记录数:{0} " , CType ( Me .DataGridView1.DataSource, DataTable).Rows.Count)
End Sub
End Class
Private WithEvents gUpdateDatabaseService As New RegionalCodeCenter.UpdateDatabaseService
Private WithEvents gServerServices As RegionalCodeCenter.Services.ServerServices
Private gLogonInformations As New LzmTW.uSystem.uData.uSql.LoginInformations
Private gRegionalCodeDatasFile As String = AppDomain.CurrentDomain.SetupInformation.ApplicationBase & " RegionalCodeDatas.xml "
Private Sub FormTestUpdateDatabase_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
gLogonInformations.Database = " RegionalCodeWorks "
gServerServices = New RegionalCodeCenter.Services.ServerServices(gLogonInformations.ConnectionStringBuilder.ConnectionString)
End Sub
Private Sub gUpdateDatabaseService_ServiceMessage( ByVal sender As Object , ByVal message As String ) Handles gUpdateDatabaseService.ServiceMessage, gServerServices.ServiceMessage
Me .Label1.Text = message
Windows.Forms.Application.DoEvents()
End Sub
Private Sub ButtonCloseExcel_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCloseExcel.Click
gUpdateDatabaseService.ExcelClose()
End Sub
Private Sub gServerServices_UpdateDatabaseCurrentPercent( ByVal sender As Object , ByVal currentPercent As Integer ) Handles gServerServices.UpdateDatabaseCurrentPercent
Me .ProgressBar1.Value = currentPercent
Windows.Forms.Application.DoEvents()
End Sub
Private Sub CheckedListBoxChooseVersionDatesToUpdate_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBoxChooseVersionDatesToUpdate.SelectedIndexChanged
Me .DataGridView1.DataSource = gUpdateDatabaseService.NetRegionalCodeTableDictionary( Me .CheckedListBoxChooseVersionDatesToUpdate.SelectedItem.ToString)
Me .Label2.Text = String .Format( " 记录数:{0} " , CType ( Me .DataGridView1.DataSource, DataTable).Rows.Count)
End Sub
End Class
FormTestUpdateDatabase.Net.vb
Partial
Class
FormTestUpdateDatabase
Private Sub ButtonDownloadNetInformations_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownloadNetInformations.Click
gUpdateDatabaseService.DownloadNetDataVersions()
With Me .ComboBoxNetVersionDates
.DataSource = gUpdateDatabaseService.NetDownloadInformationsTable
.DisplayMember = " VersionDate "
End With
End Sub
Private Sub ButtonNetDownLoadRegionalCodeByChooseVersionDate_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNetDownLoadRegionalCodeByChooseVersionDate.Click
gUpdateDatabaseService.DownLoadNetRegionalCodeDatas( Me .ComboBoxNetVersionDates.Text)
End Sub
Private Sub ButtonDownloadAllRegionalCodeDatas_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownloadAllRegionalCodeDatas.Click
gUpdateDatabaseService.DownLoadAllNetRegionalCodeDatas()
End Sub
Private Sub ButtonSaveToLocal_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveToLocal.Click
gUpdateDatabaseService.SaveDatasToFile(gRegionalCodeDatasFile)
End Sub
End Class
Private Sub ButtonDownloadNetInformations_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownloadNetInformations.Click
gUpdateDatabaseService.DownloadNetDataVersions()
With Me .ComboBoxNetVersionDates
.DataSource = gUpdateDatabaseService.NetDownloadInformationsTable
.DisplayMember = " VersionDate "
End With
End Sub
Private Sub ButtonNetDownLoadRegionalCodeByChooseVersionDate_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNetDownLoadRegionalCodeByChooseVersionDate.Click
gUpdateDatabaseService.DownLoadNetRegionalCodeDatas( Me .ComboBoxNetVersionDates.Text)
End Sub
Private Sub ButtonDownloadAllRegionalCodeDatas_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownloadAllRegionalCodeDatas.Click
gUpdateDatabaseService.DownLoadAllNetRegionalCodeDatas()
End Sub
Private Sub ButtonSaveToLocal_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveToLocal.Click
gUpdateDatabaseService.SaveDatasToFile(gRegionalCodeDatasFile)
End Sub
End Class
FormTestUpdateDatabase.Update.vb
Partial
Class
FormTestUpdateDatabase
Private Sub ButtonLoadRegionCodeDatasFormLocal_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoadRegionCodeDatasFormLocal.Click
gUpdateDatabaseService.LoadDatasFromFile( Me .gRegionalCodeDatasFile)
Dim mVersionDateArray(gUpdateDatabaseService.NetRegionalCodeTableDictionary.Count - 1 ) As String
gUpdateDatabaseService.NetRegionalCodeTableDictionary.Keys.CopyTo(mVersionDateArray, 0 )
With Me .CheckedListBoxChooseVersionDatesToUpdate.Items
.Clear()
.AddRange(mVersionDateArray)
End With
End Sub
Private Sub ButtonUpdateChooseTables_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdateChooseTables.Click
gUpdateDatabaseService.UpdateDatabaseDatasetClear()
For Each VersionDate As String In Me .CheckedListBoxChooseVersionDatesToUpdate.CheckedItems
gUpdateDatabaseService.AppendTableToUpdateDatabaseDataset(VersionDate)
Next
gServerServices.UpdateDatabase(gUpdateDatabaseService.UpdateDatabaseDataSet)
End Sub
Private Sub ButtonUpdateAllTables_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdateAllTables.Click
gUpdateDatabaseService.UpdateDatabaseDatasetClear()
gUpdateDatabaseService.AppendAllTablesToUpdateDatabaseDataset()
gServerServices.UpdateDatabase(gUpdateDatabaseService.UpdateDatabaseDataSet)
End Sub
End Class
Private Sub ButtonLoadRegionCodeDatasFormLocal_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoadRegionCodeDatasFormLocal.Click
gUpdateDatabaseService.LoadDatasFromFile( Me .gRegionalCodeDatasFile)
Dim mVersionDateArray(gUpdateDatabaseService.NetRegionalCodeTableDictionary.Count - 1 ) As String
gUpdateDatabaseService.NetRegionalCodeTableDictionary.Keys.CopyTo(mVersionDateArray, 0 )
With Me .CheckedListBoxChooseVersionDatesToUpdate.Items
.Clear()
.AddRange(mVersionDateArray)
End With
End Sub
Private Sub ButtonUpdateChooseTables_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdateChooseTables.Click
gUpdateDatabaseService.UpdateDatabaseDatasetClear()
For Each VersionDate As String In Me .CheckedListBoxChooseVersionDatesToUpdate.CheckedItems
gUpdateDatabaseService.AppendTableToUpdateDatabaseDataset(VersionDate)
Next
gServerServices.UpdateDatabase(gUpdateDatabaseService.UpdateDatabaseDataSet)
End Sub
Private Sub ButtonUpdateAllTables_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdateAllTables.Click
gUpdateDatabaseService.UpdateDatabaseDatasetClear()
gUpdateDatabaseService.AppendAllTablesToUpdateDatabaseDataset()
gServerServices.UpdateDatabase(gUpdateDatabaseService.UpdateDatabaseDataSet)
End Sub
End Class
测试效果图:
现在的方案组织:
现在看着,自己也想,简简单单的功能,有没有必要写这么多的文件哪?
前面的UpdateDatabaseService类,组织和代码还是零乱,一是函数起名有问题,二是逻辑散乱。后面再改。
目前的方案代码:供下载。
下一篇,行政区划程序的设计(十四),重新整理代码。