'创建数据库
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,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,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
总结以上是内存溢出为你收集整理的VB.NET压缩ACCESS数据库全部内容,希望文章能够帮你解决VB.NET压缩ACCESS数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)