我需要创建一个空的.mdb文件,以便我可以在其上运行ADO命令(不是 ADO.NET)。有没有办法使用ADO创建一个空的mdb?
答案 0 :(得分:4)
以下是一些有效的代码段:
string sADOProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
ADOX.CatalogClass cat = new ADOX.CatalogClass();
string sCreate = MainForm.sADOProvider + sFullPath;
cat.Create(sCreate);
// The point of this code is to unlock the access file after we
// create it. You can tell it is unlocked if the .ldb file disappears.
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
cat = null;
GC.Collect();
答案 1 :(得分:0)
不确定是否通过ADO直接创建它,但如果在计算机上安装了Access,则可以使用Access通过COM创建文件。
下面的是一个早期和晚期的例子。两种方法都有其优点/缺点。
Option Explicit
Sub CreateMDBEarlyBound()
'' Remember to set your reference to "Microsoft Access XX.0 Object Library"
Dim acApp As Access.Application
Set acApp = New Access.Application
acApp.NewCurrentDatabase ("c:\temp\MyDB-early.mdb")
Set acApp = Nothing
End Sub
Sub CreateMDBLateBound()
Dim acApp As Object
On Error Resume Next
Set acApp = GetObject(, "Access.Application")
On Error GoTo 0 '' turn off the resume next
If acApp Is Nothing Then
Set acApp = CreateObject("Access.Application")
End If
acApp.NewCurrentDatabase "c:\temp\MyDB-late.mdb"
Set acApp = Nothing
End Sub
答案 2 :(得分:0)
如果“不是ADO.NET”暗示“不是.NET”,这里的Corey Trager的代码重写为VBA:
Const sADOProvider As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Const sFullPath As String = "C:\DeleteMe.mdb"
Dim cat As ADOX.Catalog
Set cat = New ADOX.Catalog
Dim sCreate As String
sCreate = sADOProvider & sFullPath
cat.Create sCreate
' The point of this code is to unlock the access file after we
' create it. You can tell it is unlocked if the .ldb file disappears.
Set cat.ActiveConnection = Nothing