2008-11-17 85 views
2

我需要创建一个空的.mdb文件,以便我可以在其上运行ADO命令(而不是 ADO.NET)。有没有办法使用ADO创建一个空的mdb?如何从ADO创建空的MDB

回答

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(); 
+0

ReleaseComObject的解决我的问题。谢谢。 – Ron 2009-04-28 19:53:05

0

不知道有关通过ADO直接创建它,但如果安装在机器上访问你可以使用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 
0

万一 “不ADO.NET” 意味着 “不.NET”,这里的科里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