VB.NET压缩ACCESS数据库

'创建数据库

  Private Sub Create(ByVal mdbPath As String)
        If File.Exists(mdbPath) Then
            Throw New Exception("目标数据库已经存在,无法创建")
        End If
        mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath
        Dim cat As ADOX.Catalog = New ADOX.Catalog()
        cat.Create(mdbPath)
    End Sub

 

    '''' <summary>
    '''压缩修复数据库
    ''' </summary>
    ''' <param name="mdbPath"> 被压缩的数据库路径+数据库文件名</param>
    ''' <remarks></remarks>
    Public Sub Compact(ByVal mdbPath As String)
        'If Not File.Exists(mdbPath) = False Then '检查数据库是否已存在
        '    Throw New Exception("目标数据库不存在,无法压缩")
        'End If

        Dim temp = GetSysDatePartTime()

        Dim FilePath As String = mdbPath.Substring(0, mdbPath.LastIndexOf("/") + 1)
        Dim DbFileName As String = mdbPath.Substring(mdbPath.LastIndexOf("/") + 1, mdbPath.Length - mdbPath.LastIndexOf("/") - 1)

        File.Copy(mdbPath, FilePath + "DbBak/Bak" + temp + ".mdb", True)
        Dim StrProvid As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

 

        Dim jt As JRO.JetEngineClass = New JRO.JetEngineClass()

        jt.CompactDatabase(StrProvid + mdbPath, StrProvid + FilePath + "/DbBak" + temp + ".mdb")

        File.Delete(mdbPath)

        File.Copy(FilePath + "/DbBak" + temp + ".mdb", mdbPath, True)

        File.Delete(FilePath + "/DbBak" + temp + ".mdb")

        '//检查数据库是否已存在           {            throw new Exception("目标数据库不存在,无法压缩");           }        //声明临时数据库的名称        string temp = DateTime.Now.Year.ToString();          string temp2=null;           temp += DateTime.Now.Month.ToString();           temp += DateTime.Now.Day.ToString();           temp += DateTime.Now.Hour.ToString();           temp += DateTime.Now.Minute.ToString();           temp += DateTime.Now.Second.ToString() + ".bak";           temp = mdbPath.Substring(0, mdbPath.LastIndexOf("//") + 1) + temp;        //定义临时数据库的连接字符串           temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;        //定义目标数据库的连接字符串            string mdbPath2=null;           mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;        //创建一个JetEngineClass对象的实例           JRO.JetEngineClass jt = new JRO.JetEngineClass();        //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库           jt.CompactDatabase(mdbPath2, temp2);        //拷贝临时数据库到目标数据库(覆盖)           File.Copy(temp, mdbPath, true);        //最后删除临时数据库           File.Delete(temp);
    End Sub
   

    '备份数据库,mdb1,源数据库绝对路径; mdb2: 目标数据库绝对路径
    Public Sub BackUp(ByVal mdb1 As String, ByVal mdb2 As String)
        If File.Exists(mdb1) = False Then
            Throw New Exception("源数据库不存在")
        End If
        Try
            File.Copy(mdb1, mdb2, True)
        Catch ixp As IOException
            Throw New Exception(ixp.ToString())
        End Try
    End Sub


    '恢复数据库,mdb1为备份数据库绝对路径,mdb2为当前数据库绝对路径
    Public Sub Recover(ByVal mdb1 As String, ByVal mdb2 As String)
        If File.Exists(mdb1) = False Then
            Throw New Exception("备份数据库不存在")
        End If
        Try
            File.Copy(mdb1, mdb2, True)
        Catch ixp As IOException
            Throw New Exception(ixp.ToString())
        End Try
    End Sub

 

 ''' <summary>
    ''' 检查模板文件是否存在
    ''' </summary>
    ''' <param name="TempDbFile">模板文件所在路径及模板文件(全局路径)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckTempletFileExists(ByVal TempDbFile As String)
        Return File.Exists(TempDbFile)
    End Function

 

    ''' <summary>
    ''' 复制文件到新位置
    ''' </summary>
    ''' <param name="SourceFile">源文件</param>
    ''' <param name="DestcentFile">目标文件</param>
    ''' <remarks></remarks>
    Public Sub CopyDbToNewPos(ByVal SourceFile, ByVal DestcentFile)
        Try
            File.Copy(SourceFile, DestcentFile, True)
        Catch ixp As IOException
            Throw New Exception(ixp.ToString())
        End Try
    End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值