2

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

4

3 回答 3

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();
于 2008-11-17T05:39:39.370 回答
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
于 2008-11-17T05:57:40.693 回答
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
于 2009-01-08T12:32:58.623 回答