数据库在程序代码编写中往往都是形影不离的。一个数据完善的数据库支撑着程序的运行。如果数据过于庞大的话,就需要对这些必要的不可删除数据进行压缩。那么下面我们就来对VB.NET数据库压缩方法进行一个详细解读。#t#
当我们往数据库中添加数据,然后删除,如此反复,数据库就会变得很大,这样就需要对其进行压缩
下面的函数可以实现VB.NET数据库压缩
- ''' < summary>
- ''' 压缩数据库
- ''' < /summary>
- ''' < remarks>< /remarks>
- Private Sub CompactDatabase
(ByVal pFilePath As String) - Dim pFileName As String
- pFileName = GetFileName(pFilePath)
- '检查数据库文件是否存在
- If Len(Dir(pFilePath)) Then
- ' 如果需要备份就执行备份
- If MessageBox.Show("是否备份?",
"测试", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) =
Windows.Forms.DialogResult.Yes Then - wsManageGlobalParam.g_ManageFile
Control.SaveFileDialogFile.Title =
"选择保存目录" - wsManageGlobalParam.g_ManageFile
Control.SaveFileDialogFile.FileName
= pFileName - Dim pFilter As String =
GetFileExtName(pFileName) - wsManageGlobalParam.g_ManageFile
Control.SaveFileDialogFile.Filter =
"文件(*." & pFilter & ")|*." & pFilter - wsManageGlobalParam.g_ManageFileControl.
SaveFileDialogFile.ShowDialog() - Dim r As DialogResult = wsManage
GlobalParam.g_ManageFileControl.
SaveFileDialogFile.ShowDialog() - If r = Windows.Forms.DialogResult.
Cancel Then - Exit Sub
- End If
- FileCopy(pFilePath, wsManageGlobalParam.
g_ManageFileControl.SaveFileDialog
File.FileName) - End If
- wsDataSet.SetNothing()
- Dim jro As JRO.JetEngine
- Dim pTempPath As String = GetTempPath()
- jro = New JRO.JetEngine()
- jro.CompactDatabase(wsManageGlobalParam.
g_Connection & pFilePath, wsManage
GlobalParam.g_Connection & pTempPath
& "\" & pFileName) - Kill(pFilePath)
- FileCopy(pTempPath & "\" & pFileName,
pFilePath) - Kill(pTempPath & "\" & pFileName)
- End If
好了,VB.NET数据库压缩的具体实现代码就为大家介绍到这里。