'创建数据库
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
VB.NET创建/修复/压缩/备份/恢复ACCESS数据库
最新推荐文章于 2021-09-06 11:05:34 发布